mirror of https://github.com/abpframework/eventhub
14 changed files with 378 additions and 11 deletions
@ -0,0 +1,10 @@ |
|||
using Volo.Abp.BlobStoring; |
|||
|
|||
namespace EventHub.Organizations |
|||
{ |
|||
[BlobContainerName("organization-profile-pictures")] |
|||
public class OrganizationProfilePictureContainer |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,39 @@ |
|||
using System.IO; |
|||
using System.Threading.Tasks; |
|||
using EventHub.Organizations; |
|||
using EventHub.Web.Pages.Organizations; |
|||
using Microsoft.AspNetCore.Authorization; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using Volo.Abp.AspNetCore.Mvc; |
|||
|
|||
namespace EventHub.Web.Controllers |
|||
{ |
|||
[Route("api/organization")] |
|||
public class OrganizationController : AbpController |
|||
{ |
|||
private readonly IOrganizationAppService _organizationAppService; |
|||
|
|||
public OrganizationController(IOrganizationAppService organizationAppService) |
|||
{ |
|||
_organizationAppService = organizationAppService; |
|||
} |
|||
|
|||
[HttpPost] |
|||
[Authorize] |
|||
public async Task SaveProfilePicture([FromForm] OrganizationProfilePictureInput input) |
|||
{ |
|||
byte[] profilePictureContent = new byte[] {}; |
|||
|
|||
if (input.ProfilePictureFile != null && input.ProfilePictureFile.Length > 0) |
|||
{ |
|||
using (var memoryStream = new MemoryStream()) |
|||
{ |
|||
await input.ProfilePictureFile.CopyToAsync(memoryStream); |
|||
profilePictureContent = memoryStream.ToArray(); |
|||
} |
|||
} |
|||
|
|||
await _organizationAppService.SaveProfilePictureAsync(input.OrganizationId, profilePictureContent); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,38 @@ |
|||
using System.ComponentModel.DataAnnotations; |
|||
using System.IO; |
|||
using System.Linq; |
|||
using Microsoft.AspNetCore.Http; |
|||
|
|||
namespace EventHub.Web.Helpers |
|||
{ |
|||
public class AllowedExtensionsAttribute : ValidationAttribute |
|||
{ |
|||
private readonly string[] _allowedExtensions; |
|||
|
|||
public AllowedExtensionsAttribute(string[] allowedExtensions) |
|||
{ |
|||
_allowedExtensions = allowedExtensions; |
|||
} |
|||
|
|||
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext) |
|||
{ |
|||
if (value == null) |
|||
{ |
|||
return ValidationResult.Success; |
|||
} |
|||
|
|||
var file = value as IFormFile; |
|||
var extension = Path.GetExtension(file.FileName); |
|||
|
|||
if (file.Length > 0 && file != null) |
|||
{ |
|||
if (!_allowedExtensions.Contains(extension.ToLower())) |
|||
{ |
|||
return new ValidationResult("This file extension is not allowed. Allowed extensions: png, jpg, jpeg"); |
|||
} |
|||
} |
|||
|
|||
return ValidationResult.Success; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
using System.ComponentModel.DataAnnotations; |
|||
using Microsoft.AspNetCore.Http; |
|||
|
|||
namespace EventHub.Web.Helpers |
|||
{ |
|||
public class MaxFileSizeAttribute : ValidationAttribute |
|||
{ |
|||
private readonly int _maxFileSize; |
|||
|
|||
public MaxFileSizeAttribute(int maxFileSize) |
|||
{ |
|||
_maxFileSize = maxFileSize; |
|||
} |
|||
|
|||
protected override ValidationResult IsValid( |
|||
object value, ValidationContext validationContext) |
|||
{ |
|||
if (value == null) |
|||
{ |
|||
return ValidationResult.Success; |
|||
} |
|||
|
|||
var file = value as IFormFile; |
|||
|
|||
if (file.Length > 0) |
|||
{ |
|||
if (file.Length > _maxFileSize) |
|||
{ |
|||
return new ValidationResult("Maximum file size: 1MB"); |
|||
} |
|||
} |
|||
|
|||
return ValidationResult.Success; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,113 @@ |
|||
$(function () { |
|||
var l = abp.localization.getResource('EventHub'); |
|||
|
|||
var fileInput = document.getElementById("organization_id"); |
|||
var file; |
|||
|
|||
fileInput.addEventListener('change', function () { |
|||
var showModal = true; |
|||
|
|||
file = fileInput.files[0]; |
|||
|
|||
if(file == undefined) { |
|||
$("#choose-cover-image").html("Choose a cover image..."); |
|||
return; |
|||
} |
|||
else { |
|||
$("#choose-cover-image").html(file.name); |
|||
} |
|||
|
|||
var permittedExtensions = ["jpg", "jpeg", "png"] |
|||
var fileExtension = $(this).val().split('.').pop(); |
|||
|
|||
if (permittedExtensions.indexOf(fileExtension) === -1) { |
|||
showModal = false; |
|||
abp.message.error('This Extension Is Not Allowed') |
|||
.then(() => { |
|||
$("#choose-cover-image").html("Choose a cover image..."); |
|||
file = null; |
|||
}); |
|||
} |
|||
//1mb
|
|||
else if(file.size > 1024 * 1024) { |
|||
showModal = false; |
|||
abp.message.error('The File Is Too Large') |
|||
.then(() => { |
|||
$("#choose-cover-image").html("Choose a cover image..."); |
|||
file = null; |
|||
}); |
|||
} |
|||
|
|||
var img = new Image(); |
|||
img.onload = function () { |
|||
var imageError = true; |
|||
var sizes = { |
|||
width: this.width, |
|||
height: this.height |
|||
}; |
|||
|
|||
URL.revokeObjectURL(this.src); |
|||
|
|||
if(showModal && imageError) { |
|||
readURL(file); |
|||
$("#picturePreviewModal").modal('show'); |
|||
} |
|||
} |
|||
|
|||
var objectURL = URL.createObjectURL(file); |
|||
img.src = objectURL; |
|||
}); |
|||
|
|||
function readURL(input) { |
|||
if (input) { |
|||
var reader = new FileReader(); |
|||
|
|||
reader.onload = function (e) { |
|||
$('#img-upload').attr('src', e.target.result); |
|||
} |
|||
|
|||
reader.readAsDataURL(input); |
|||
} |
|||
} |
|||
|
|||
$("form#EditOrganizationProfileForm").submit(function (e) { |
|||
e.preventDefault(); |
|||
|
|||
var id = document.getElementById("my-id").value; |
|||
|
|||
var formData = new FormData(); |
|||
formData.append("OrganizationId", id); |
|||
formData.append("ProfilePictureFile", file); |
|||
|
|||
$.ajax({ |
|||
xhr: function() { |
|||
var xhr = new window.XMLHttpRequest(); |
|||
xhr.upload.addEventListener("progress", function(evt) { |
|||
if (evt.lengthComputable) { |
|||
var percentComplete = evt.loaded / evt.total; |
|||
percentComplete = parseInt(percentComplete * 100); |
|||
if(percentComplete !== 100){ |
|||
$('#btnSubmit').prop( "disabled", true ); |
|||
} |
|||
} |
|||
}, false); |
|||
|
|||
return xhr; |
|||
}, |
|||
url: `/api/organization`, |
|||
data: formData, |
|||
type: 'POST', |
|||
contentType: false, |
|||
processData: false, |
|||
success: function(data){ |
|||
abp.message |
|||
.success("Organization Profile Picture successfully added.") |
|||
.then(data => window.location.href = "/"); |
|||
}, |
|||
error: function (data) { |
|||
abp.message.error(data.responseJSON.error.message); |
|||
} |
|||
}); |
|||
|
|||
}); |
|||
}); |
|||
Loading…
Reference in new issue