-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolved #810: Blog Module's FilesController should support N-tier de…
…ployment.
- Loading branch information
Showing
17 changed files
with
185 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Files/BloggingWebConsts.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
|
||
namespace Volo.Blogging | ||
{ | ||
public class BloggingWebConsts | ||
{ | ||
public class FileUploading | ||
{ | ||
public const int MaxFileSize = 5242880; //5MB | ||
|
||
public static int MaxFileSizeAsMegabytes => Convert.ToInt32((MaxFileSize / 1024f) / 1024f); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...logging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Files/FileUploadInputDto.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace Volo.Blogging.Files | ||
{ | ||
public class FileUploadInputDto | ||
{ | ||
[Required] | ||
public byte[] Bytes { get; set; } | ||
|
||
[Required] | ||
public string Name { get; set; } | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...ogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Files/FileUploadOutputDto.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Volo.Blogging.Files | ||
{ | ||
public class FileUploadOutputDto | ||
{ | ||
public string Url { get; set; } | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...s/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Files/IFileAppService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Threading.Tasks; | ||
using Volo.Abp.Application.Services; | ||
|
||
namespace Volo.Blogging.Files | ||
{ | ||
public interface IFileAppService : IApplicationService | ||
{ | ||
Task<FileUploadOutputDto> UploadAsync(FileUploadInputDto input); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 1 addition & 4 deletions
5
modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/CommonOperations.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Files/BlogFileOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace Volo.Blogging.Files | ||
{ | ||
/* TODO: | ||
* - It is not to have different options for all different modules. We should find a more generic way. | ||
* - Actually, it is not good to assume to save to a local folder. Instead, use file storage once implemented. | ||
*/ | ||
public class BlogFileOptions | ||
{ | ||
public string FileUploadLocalFolder { get; set; } | ||
|
||
public string FileUploadUrlRoot { get; set; } | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Files/FileAppService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Options; | ||
using Volo.Abp; | ||
using Volo.Abp.Application.Services; | ||
using Volo.Abp.Validation; | ||
using Volo.Blogging.Areas.Blog.Helpers; | ||
|
||
namespace Volo.Blogging.Files | ||
{ | ||
public class FileAppService : ApplicationService, IFileAppService | ||
{ | ||
public BlogFileOptions Options { get; } | ||
|
||
public FileAppService(IOptions<BlogFileOptions> options) | ||
{ | ||
Options = options.Value; | ||
} | ||
|
||
public virtual Task<FileUploadOutputDto> UploadAsync(FileUploadInputDto input) | ||
{ | ||
if (input.Bytes.IsNullOrEmpty()) | ||
{ | ||
ThrowValidationException("Bytes can not be null or empty!", "Bytes"); | ||
} | ||
|
||
if (input.Bytes.Length > BloggingWebConsts.FileUploading.MaxFileSize) | ||
{ | ||
throw new UserFriendlyException($"File exceeds the maximum upload size ({BloggingWebConsts.FileUploading.MaxFileSizeAsMegabytes} MB)!"); | ||
} | ||
|
||
if (!ImageFormatHelper.IsValidImage(input.Bytes, FileUploadConsts.AllowedImageUploadFormats)) | ||
{ | ||
throw new UserFriendlyException("Not a valid image format!"); | ||
} | ||
|
||
var uniqueFileName = GenerateUniqueFileName(Path.GetExtension(input.Name)); | ||
var filePath = Path.Combine(Options.FileUploadLocalFolder, uniqueFileName); | ||
|
||
File.WriteAllBytes(filePath, input.Bytes); //TODO: Previously was using WriteAllBytesAsync, but it's only in .netcore. | ||
|
||
return Task.FromResult(new FileUploadOutputDto | ||
{ | ||
Url = Options.FileUploadUrlRoot.EnsureEndsWith('/') + uniqueFileName | ||
}); | ||
} | ||
|
||
private static void ThrowValidationException(string message, string memberName) | ||
{ | ||
throw new AbpValidationException(message, | ||
new List<ValidationResult> | ||
{ | ||
new ValidationResult(message, new[] {memberName}) | ||
}); | ||
} | ||
|
||
protected virtual string GenerateUniqueFileName(string extension, string prefix = null, string postfix = null) | ||
{ | ||
return prefix + GuidGenerator.Create().ToString("N") + postfix + extension; | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Files/FileUploadConsts.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.Drawing.Imaging; | ||
using System.Linq; | ||
|
||
namespace Volo.Blogging.Files | ||
{ | ||
public class FileUploadConsts | ||
{ | ||
public static readonly ICollection<ImageFormat> AllowedImageUploadFormats = new Collection<ImageFormat> | ||
{ | ||
ImageFormat.Jpeg, | ||
ImageFormat.Png, | ||
ImageFormat.Gif, | ||
ImageFormat.Bmp | ||
}; | ||
|
||
public static string AllowedImageFormatsJoint => string.Join(",", AllowedImageUploadFormats.Select(x => x.ToString())); | ||
} | ||
} |
File renamed without changes.
36 changes: 30 additions & 6 deletions
36
modules/blogging/src/Volo.Blogging.Web/Areas/Blog/Controllers/FilesController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,55 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Volo.Abp; | ||
using Volo.Abp.AspNetCore.Mvc; | ||
using Volo.Blogging.Areas.Blog.Models; | ||
using Volo.Blogging.Files; | ||
using Volo.Blogging.Hosting; | ||
|
||
namespace Volo.Blogging.Areas.Blog.Controllers | ||
{ | ||
//TODO: This may be moved to HttpApi project since it may be needed by a SPA too. | ||
[Area("Blog")] | ||
[Route("Blog/[controller]/[action]")] | ||
public class FilesController : AbpController | ||
{ | ||
private readonly IFileService _fileService; | ||
private readonly IFileAppService _fileAppService; | ||
|
||
public FilesController(IFileService fileService) | ||
public FilesController(IFileAppService fileAppService) | ||
{ | ||
_fileService = fileService; | ||
_fileAppService = fileAppService; | ||
} | ||
|
||
[HttpPost] | ||
public async Task<JsonResult> UploadImage(IFormFile file) | ||
{ | ||
file.ValidateImage(out var fileBytes); | ||
//TODO: localize exception messages | ||
|
||
var fileUrl = await _fileService.SaveFileAsync(fileBytes, file.FileName); | ||
if (file == null) | ||
{ | ||
throw new UserFriendlyException("No file found!"); | ||
} | ||
|
||
return Json(new FileUploadResult(fileUrl)); | ||
if (file.Length <= 0) | ||
{ | ||
throw new UserFriendlyException("File is empty!"); | ||
} | ||
|
||
if (!file.ContentType.Contains("image")) | ||
{ | ||
throw new UserFriendlyException("Not a valid image!"); | ||
} | ||
|
||
var output = await _fileAppService.UploadAsync( | ||
new FileUploadInputDto | ||
{ | ||
Bytes = file.AsBytes(), | ||
Name = file.FileName | ||
} | ||
); | ||
|
||
return Json(new FileUploadResult(output.Url)); | ||
} | ||
} | ||
} |
30 changes: 0 additions & 30 deletions
30
modules/blogging/src/Volo.Blogging.Web/BloggingWebConsts.cs
This file was deleted.
Oops, something went wrong.
72 changes: 0 additions & 72 deletions
72
modules/blogging/src/Volo.Blogging.Web/Hosting/FileService.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.