Compare commits
10
Commits
233d3ef369
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6e701376df | ||
|
|
41f220d145 | ||
|
|
c3b9b3e43a | ||
|
|
8a66b8b7e9 | ||
|
|
7e08427539 | ||
|
|
b26767e126 | ||
|
|
83b907988b | ||
|
|
dd23ed4f64 | ||
|
|
330a3958f3 | ||
|
|
d85dbcfdf7 |
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
@@ -11,7 +12,8 @@ using xStorageService.Models;
|
||||
|
||||
namespace xStorageService.Interfaces
|
||||
{
|
||||
public partial interface IXStorageProvider {
|
||||
public partial interface IXStorageProvider
|
||||
{
|
||||
XStorageConfiguration Configuration { get; }
|
||||
XStorageHelper StorageHelper { get; }
|
||||
IFileProvider FileProvider { get; }
|
||||
@@ -29,6 +31,7 @@ namespace xStorageService.Interfaces
|
||||
string GetVideoFolderPath();
|
||||
string GetDocumentFolderPath();
|
||||
string FilePathResolver(string fullPath);
|
||||
string ConvertToFileUrl(string fullPath);
|
||||
#endregion
|
||||
|
||||
//
|
||||
@@ -79,7 +82,8 @@ namespace xStorageService.Interfaces
|
||||
Task<string> SaveFileAsync(
|
||||
IFormFile file,
|
||||
bool isTemp = false,
|
||||
bool isWidget = false
|
||||
bool isWidget = false,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
void DeleteFile(
|
||||
string fileFullPath,
|
||||
@@ -92,11 +96,26 @@ namespace xStorageService.Interfaces
|
||||
Exception exception = null
|
||||
);
|
||||
// Task CreateThumbnail (XFile file);
|
||||
Task CreateThumbnail (XFileDto file);
|
||||
Task CreateThumbnail (string fileName);
|
||||
Task<XFileResult> HandleProfileImageSave (IFormFile file);
|
||||
Task<XFileResult> HandleFileSave (IFormFile file);
|
||||
Task<IEnumerable<XFileResult>> HandleFilesSave (IFormFileCollection files);
|
||||
Task CreateThumbnail(
|
||||
XFileDto file,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
Task CreateThumbnail(
|
||||
string fileName,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
Task<XFileResult> HandleProfileImageSave(
|
||||
IFormFile file,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
Task<XFileResult> HandleFileSave(
|
||||
IFormFile file,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
Task<IEnumerable<XFileResult>> HandleFilesSave(
|
||||
IFormFileCollection files,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
+257
-115
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
@@ -15,8 +16,10 @@ using xStorageService.Helpers;
|
||||
using xStorageService.Interfaces;
|
||||
using xStorageService.Models;
|
||||
|
||||
namespace xStorageService.Providers {
|
||||
public partial class XStorageProvider : IXStorageProvider {
|
||||
namespace xStorageService.Providers
|
||||
{
|
||||
public partial class XStorageProvider : IXStorageProvider
|
||||
{
|
||||
|
||||
public readonly string BasePath;
|
||||
private readonly XFileType[] ThumbSupprtedTypes = new XFileType[] {
|
||||
@@ -33,7 +36,8 @@ namespace xStorageService.Providers {
|
||||
XStorageHelper storageHelper,
|
||||
IXThumbnailProvider thumbnailProvider,
|
||||
XStorageConfiguration configuration
|
||||
) {
|
||||
)
|
||||
{
|
||||
//
|
||||
FileProvider = fileProvider;
|
||||
Configuration = configuration;
|
||||
@@ -52,7 +56,8 @@ namespace xStorageService.Providers {
|
||||
/// Get Storage Folder Path
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetStorageFolderPath () {
|
||||
public string GetStorageFolderPath()
|
||||
{
|
||||
return Path.Combine(BasePath, Configuration.Storage);
|
||||
}
|
||||
|
||||
@@ -60,7 +65,8 @@ namespace xStorageService.Providers {
|
||||
/// Get Temp Folder Path
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetTempFolderPath () {
|
||||
public string GetTempFolderPath()
|
||||
{
|
||||
return Path.Combine(GetStorageFolderPath(), Configuration.Temp);
|
||||
}
|
||||
|
||||
@@ -68,7 +74,8 @@ namespace xStorageService.Providers {
|
||||
/// Get Uploads Folder Path
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetUploadsFolderPath () {
|
||||
public string GetUploadsFolderPath()
|
||||
{
|
||||
return Path.Combine(GetStorageFolderPath(), Configuration.Uploads);
|
||||
}
|
||||
|
||||
@@ -76,7 +83,8 @@ namespace xStorageService.Providers {
|
||||
/// Get Widgets Folder Path
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetWidgetsFolderPath () {
|
||||
public string GetWidgetsFolderPath()
|
||||
{
|
||||
return Path.Combine(GetStorageFolderPath(), Configuration.Widgets);
|
||||
}
|
||||
|
||||
@@ -84,7 +92,8 @@ namespace xStorageService.Providers {
|
||||
/// Get Thumb Folder Path
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetThumbFolderPath () {
|
||||
public string GetThumbFolderPath()
|
||||
{
|
||||
return Path.Combine(GetUploadsFolderPath(), Configuration.Thumb);
|
||||
}
|
||||
|
||||
@@ -92,7 +101,8 @@ namespace xStorageService.Providers {
|
||||
/// Get Images Folder Path
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetImageFolderPath () {
|
||||
public string GetImageFolderPath()
|
||||
{
|
||||
return Path.Combine(GetUploadsFolderPath(), Configuration.Image);
|
||||
}
|
||||
|
||||
@@ -100,7 +110,8 @@ namespace xStorageService.Providers {
|
||||
/// Get Audios Folder Path
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetAudioFolderPath () {
|
||||
public string GetAudioFolderPath()
|
||||
{
|
||||
return Path.Combine(GetUploadsFolderPath(), Configuration.Audio);
|
||||
}
|
||||
|
||||
@@ -108,7 +119,8 @@ namespace xStorageService.Providers {
|
||||
/// Get Videos Folder Path
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetVideoFolderPath () {
|
||||
public string GetVideoFolderPath()
|
||||
{
|
||||
return Path.Combine(GetUploadsFolderPath(), Configuration.Video);
|
||||
}
|
||||
|
||||
@@ -116,7 +128,8 @@ namespace xStorageService.Providers {
|
||||
/// Get Documents Folder Path
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetDocumentFolderPath () {
|
||||
public string GetDocumentFolderPath()
|
||||
{
|
||||
return Path.Combine(GetUploadsFolderPath(), Configuration.Document);
|
||||
}
|
||||
|
||||
@@ -125,7 +138,8 @@ namespace xStorageService.Providers {
|
||||
/// </summary>
|
||||
/// <param name="fullPath"></param>
|
||||
/// <returns></returns>
|
||||
public string FilePathResolver (string fullPath) {
|
||||
public string FilePathResolver(string fullPath)
|
||||
{
|
||||
return fullPath.Replace(BasePath, "");
|
||||
}
|
||||
|
||||
@@ -134,7 +148,8 @@ namespace xStorageService.Providers {
|
||||
/// </summary>
|
||||
/// <param name="fullPath"></param>
|
||||
/// <returns></returns>
|
||||
public string ConvertToProfileImageUrl (string fullPath) {
|
||||
public string ConvertToProfileImageUrl(string fullPath)
|
||||
{
|
||||
//
|
||||
var authority = !Configuration.IdentityAuthority.IsNullOrEmpty() &&
|
||||
Configuration.IdentityAuthority.EndsWith("/") ?
|
||||
@@ -150,7 +165,8 @@ namespace xStorageService.Providers {
|
||||
/// </summary>
|
||||
/// <param name="fullPath"></param>
|
||||
/// <returns></returns>
|
||||
public string ConvertToFileUrl (string fullPath) {
|
||||
public string ConvertToFileUrl(string fullPath)
|
||||
{
|
||||
//
|
||||
var authority = !Configuration.Authority.IsNullOrEmpty() &&
|
||||
Configuration.Authority.EndsWith("/") ?
|
||||
@@ -168,63 +184,76 @@ namespace xStorageService.Providers {
|
||||
/// Check Folder Structure of Storage Area
|
||||
/// and Create them if not Exists
|
||||
/// </summary>
|
||||
public void Prepare () {
|
||||
public void Prepare()
|
||||
{
|
||||
//
|
||||
try {
|
||||
try
|
||||
{
|
||||
//
|
||||
// Storage Folder Path Check ...
|
||||
if (!IsFolderExists (GetStorageFolderPath ())) {
|
||||
if (!IsFolderExists(GetStorageFolderPath()))
|
||||
{
|
||||
Directory.CreateDirectory(GetStorageFolderPath());
|
||||
}
|
||||
|
||||
//
|
||||
// Temp Folder Path Check ...
|
||||
if (!IsFolderExists (GetTempFolderPath ())) {
|
||||
if (!IsFolderExists(GetTempFolderPath()))
|
||||
{
|
||||
Directory.CreateDirectory(GetTempFolderPath());
|
||||
}
|
||||
|
||||
//
|
||||
// Temp Folder Path Check ...
|
||||
if (!IsFolderExists (GetThumbFolderPath ())) {
|
||||
if (!IsFolderExists(GetThumbFolderPath()))
|
||||
{
|
||||
Directory.CreateDirectory(GetThumbFolderPath());
|
||||
}
|
||||
|
||||
//
|
||||
// Widgets Folder Path Chekc ...
|
||||
if (!IsFolderExists (GetWidgetsFolderPath ())) {
|
||||
if (!IsFolderExists(GetWidgetsFolderPath()))
|
||||
{
|
||||
Directory.CreateDirectory(GetWidgetsFolderPath());
|
||||
}
|
||||
|
||||
//
|
||||
// Uploads Folder Path Chekc ...
|
||||
if (!IsFolderExists (GetUploadsFolderPath ())) {
|
||||
if (!IsFolderExists(GetUploadsFolderPath()))
|
||||
{
|
||||
Directory.CreateDirectory(GetUploadsFolderPath());
|
||||
}
|
||||
|
||||
//
|
||||
// Image Folder Path Chekc ...
|
||||
if (!IsFolderExists (GetImageFolderPath ())) {
|
||||
if (!IsFolderExists(GetImageFolderPath()))
|
||||
{
|
||||
Directory.CreateDirectory(GetImageFolderPath());
|
||||
}
|
||||
|
||||
//
|
||||
// Music Folder Path Chekc ...
|
||||
if (!IsFolderExists (GetAudioFolderPath ())) {
|
||||
if (!IsFolderExists(GetAudioFolderPath()))
|
||||
{
|
||||
Directory.CreateDirectory(GetAudioFolderPath());
|
||||
}
|
||||
|
||||
//
|
||||
// Video Folder Path Chekc ...
|
||||
if (!IsFolderExists (GetVideoFolderPath ())) {
|
||||
if (!IsFolderExists(GetVideoFolderPath()))
|
||||
{
|
||||
Directory.CreateDirectory(GetVideoFolderPath());
|
||||
}
|
||||
|
||||
//
|
||||
// Document Folder Path Chekc ...
|
||||
if (!IsFolderExists (GetDocumentFolderPath ())) {
|
||||
if (!IsFolderExists(GetDocumentFolderPath()))
|
||||
{
|
||||
Directory.CreateDirectory(GetDocumentFolderPath());
|
||||
}
|
||||
} catch {
|
||||
}
|
||||
catch
|
||||
{
|
||||
XException.StorageServiceInitialFailed.Throw();
|
||||
}
|
||||
|
||||
@@ -241,7 +270,8 @@ namespace xStorageService.Providers {
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <returns></returns>
|
||||
public bool IsFolderExists (string path) {
|
||||
public bool IsFolderExists(string path)
|
||||
{
|
||||
return Directory.Exists(path);
|
||||
}
|
||||
|
||||
@@ -250,7 +280,8 @@ namespace xStorageService.Providers {
|
||||
/// </summary>
|
||||
/// <param name="filePath"></param>
|
||||
/// <returns></returns>
|
||||
public bool IsFileExists (string filePath) {
|
||||
public bool IsFileExists(string filePath)
|
||||
{
|
||||
return File.Exists(filePath);
|
||||
}
|
||||
|
||||
@@ -259,9 +290,11 @@ namespace xStorageService.Providers {
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <returns></returns>
|
||||
public bool IsSupportThumb (string fileName) {
|
||||
public bool IsSupportThumb(string fileName)
|
||||
{
|
||||
//
|
||||
if (!fileName.StartsWith (Configuration.FilePrefix)) {
|
||||
if (!fileName.StartsWith(Configuration.FilePrefix))
|
||||
{
|
||||
XException.InavlidFile.Throw();
|
||||
}
|
||||
|
||||
@@ -275,7 +308,8 @@ namespace xStorageService.Providers {
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
public bool IsSupportThumb (XFileType type) {
|
||||
public bool IsSupportThumb(XFileType type)
|
||||
{
|
||||
return ThumbSupprtedTypes.Contains(type);
|
||||
}
|
||||
|
||||
@@ -283,9 +317,11 @@ namespace xStorageService.Providers {
|
||||
/// Validate a File Exists or not
|
||||
/// </summary>
|
||||
/// <param name="filePath"></param>
|
||||
public void ValidateFileExists (string filePath) {
|
||||
public void ValidateFileExists(string filePath)
|
||||
{
|
||||
//
|
||||
if (!IsFileExists (filePath)) {
|
||||
if (!IsFileExists(filePath))
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
}
|
||||
@@ -294,24 +330,28 @@ namespace xStorageService.Providers {
|
||||
/// Validate a File Model with it's Related Physical File
|
||||
/// </summary>
|
||||
/// <param name="file"></param>
|
||||
public void ValidateFileModel (XFileDto file) {
|
||||
public void ValidateFileModel(XFileDto file)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
if (file == null) {
|
||||
if (file == null)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Retrieve Full File Path ...
|
||||
var fileFullPath = file.Path;
|
||||
if (fileFullPath.IsNullOrEmpty ()) {
|
||||
if (fileFullPath.IsNullOrEmpty())
|
||||
{
|
||||
XException.InavlidFile.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Retrieve is File Exists or not ...
|
||||
var isPhysicalFileExists = IsFileExists(file.Path);
|
||||
if (!isPhysicalFileExists) {
|
||||
if (!isPhysicalFileExists)
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
}
|
||||
@@ -320,22 +360,26 @@ namespace xStorageService.Providers {
|
||||
/// Validate File for Upload
|
||||
/// </summary>
|
||||
/// <param name="file"></param>
|
||||
public void ValidateFile (IFormFile file) {
|
||||
public void ValidateFile(IFormFile file)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
if (file == null) {
|
||||
if (file == null)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Check File Size not Zero ...
|
||||
if (file.Length == 0) {
|
||||
if (file.Length == 0)
|
||||
{
|
||||
XException.EmptyFile.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Check File Max Size ...
|
||||
if (file.Length > Configuration.MaxFileSize) {
|
||||
if (file.Length > Configuration.MaxFileSize)
|
||||
{
|
||||
XException.MaxFileSizeExceeded.Throw();
|
||||
}
|
||||
}
|
||||
@@ -344,10 +388,12 @@ namespace xStorageService.Providers {
|
||||
/// Validate a Collection of IFormFile
|
||||
/// </summary>
|
||||
/// <param name="files"></param>
|
||||
public void ValidateFiles (IFormFileCollection files) {
|
||||
public void ValidateFiles(IFormFileCollection files)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
if (!files.HasChild ()) {
|
||||
if (!files.HasChild())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
@@ -360,7 +406,8 @@ namespace xStorageService.Providers {
|
||||
/// Validate File for Profile Image
|
||||
/// </summary>
|
||||
/// <param name="file"></param>
|
||||
public void ValidateProfileImage (IFormFile file) {
|
||||
public void ValidateProfileImage(IFormFile file)
|
||||
{
|
||||
//
|
||||
// Check File Validation ...
|
||||
// since Args Checked in File Validation, there is no need to
|
||||
@@ -370,7 +417,8 @@ namespace xStorageService.Providers {
|
||||
//
|
||||
// Check File Type ...
|
||||
var fileType = GetFileType(file.FileName);
|
||||
if (fileType != XFileType.Image) {
|
||||
if (fileType != XFileType.Image)
|
||||
{
|
||||
XException.UnsupportedFileType.Throw();
|
||||
}
|
||||
}
|
||||
@@ -382,7 +430,8 @@ namespace xStorageService.Providers {
|
||||
/// Generate New File
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string NewFileName () {
|
||||
public string NewFileName()
|
||||
{
|
||||
return Configuration.FilePrefix + Guid.NewGuid().ToString().ToLower();
|
||||
}
|
||||
|
||||
@@ -391,9 +440,11 @@ namespace xStorageService.Providers {
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <returns></returns>
|
||||
public string GenerateThumbName (string fileName) {
|
||||
public string GenerateThumbName(string fileName)
|
||||
{
|
||||
//
|
||||
if (!IsSupportThumb (fileName)) {
|
||||
if (!IsSupportThumb(fileName))
|
||||
{
|
||||
XException.UnsupportedFileType.Throw();
|
||||
}
|
||||
|
||||
@@ -406,10 +457,12 @@ namespace xStorageService.Providers {
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <returns></returns>
|
||||
public string GetNewFileName (string fileName) {
|
||||
public string GetNewFileName(string fileName)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
if (fileName.IsNullOrEmpty ()) {
|
||||
if (fileName.IsNullOrEmpty())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
@@ -433,10 +486,12 @@ namespace xStorageService.Providers {
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <returns></returns>
|
||||
public string GetFileExtension (string fileName) {
|
||||
public string GetFileExtension(string fileName)
|
||||
{
|
||||
//
|
||||
var result = StorageHelper.GetFileExtension(fileName);
|
||||
if (result.IsNullOrEmpty ()) {
|
||||
if (result.IsNullOrEmpty())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
@@ -449,10 +504,12 @@ namespace xStorageService.Providers {
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <returns></returns>
|
||||
public string GetFileNameWithoutExtension (string fileName) {
|
||||
public string GetFileNameWithoutExtension(string fileName)
|
||||
{
|
||||
//
|
||||
var result = StorageHelper.GetFileNameWithoutExtension(fileName);
|
||||
if (result.IsNullOrEmpty ()) {
|
||||
if (result.IsNullOrEmpty())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
@@ -465,16 +522,19 @@ namespace xStorageService.Providers {
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <returns></returns>
|
||||
public FileExtensions GetFileExtensionType (string fileName) {
|
||||
public FileExtensions GetFileExtensionType(string fileName)
|
||||
{
|
||||
//
|
||||
var fileExt = GetFileExtension(fileName);
|
||||
if (fileExt.IsNullOrEmpty ()) {
|
||||
if (fileExt.IsNullOrEmpty())
|
||||
{
|
||||
XException.InvalidData.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var result = StorageHelper.GetExtensionType(fileExt);
|
||||
if (result == -1) {
|
||||
if (result == -1)
|
||||
{
|
||||
XException.UnsupportedFileType.Throw();
|
||||
}
|
||||
|
||||
@@ -487,7 +547,8 @@ namespace xStorageService.Providers {
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <returns></returns>
|
||||
public XFileType GetFileType (string fileName) {
|
||||
public XFileType GetFileType(string fileName)
|
||||
{
|
||||
//
|
||||
var fileExtension = GetFileExtensionType(fileName);
|
||||
return GetFileType(fileExtension);
|
||||
@@ -498,7 +559,8 @@ namespace xStorageService.Providers {
|
||||
/// </summary>
|
||||
/// <param name="fileExtension"></param>
|
||||
/// <returns></returns>
|
||||
public XFileType GetFileType (FileExtensions fileExtension) {
|
||||
public XFileType GetFileType(FileExtensions fileExtension)
|
||||
{
|
||||
return StorageHelper.GetType((int)fileExtension);
|
||||
}
|
||||
|
||||
@@ -507,9 +569,11 @@ namespace xStorageService.Providers {
|
||||
/// </summary>
|
||||
/// <param name="thumFileName"></param>
|
||||
/// <returns></returns>
|
||||
public string GetThumbnailFileFullPath (string thumFileName) {
|
||||
public string GetThumbnailFileFullPath(string thumFileName)
|
||||
{
|
||||
//
|
||||
if (!thumFileName.StartsWith (Configuration.ThumbPrefix)) {
|
||||
if (!thumFileName.StartsWith(Configuration.ThumbPrefix))
|
||||
{
|
||||
XException.InavlidFile.Throw();
|
||||
}
|
||||
|
||||
@@ -522,7 +586,8 @@ namespace xStorageService.Providers {
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <returns></returns>
|
||||
public string GetRelatedThumbnailFullPath (string fileName) {
|
||||
public string GetRelatedThumbnailFullPath(string fileName)
|
||||
{
|
||||
//
|
||||
var thumbName = GenerateThumbName(fileName);
|
||||
return GetThumbnailFileFullPath(thumbName);
|
||||
@@ -533,10 +598,12 @@ namespace xStorageService.Providers {
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
public string GetFileTypeFolderPath (XFileType type) {
|
||||
public string GetFileTypeFolderPath(XFileType type)
|
||||
{
|
||||
//
|
||||
// Retrieve Folder Path Based on file ...
|
||||
switch (type) {
|
||||
switch (type)
|
||||
{
|
||||
case XFileType.Image:
|
||||
case XFileType.CoverImage:
|
||||
case XFileType.ProfileImasge:
|
||||
@@ -564,10 +631,13 @@ namespace xStorageService.Providers {
|
||||
public string GetFileFolderPath(
|
||||
string fileName,
|
||||
bool isTemp = false,
|
||||
bool isWidget = false) {
|
||||
bool isWidget = false
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
if (fileName.IsNullOrEmpty ()) {
|
||||
if (fileName.IsNullOrEmpty())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
@@ -580,14 +650,16 @@ namespace xStorageService.Providers {
|
||||
//
|
||||
// if isTemp equals true
|
||||
// ignore file type and return Temp Folder ...
|
||||
if (isTemp) {
|
||||
if (isTemp)
|
||||
{
|
||||
folderPath = GetTempFolderPath();
|
||||
}
|
||||
|
||||
//
|
||||
// if isWidget equals true and isTemp equals false
|
||||
// ignore file type and return Widget's Folder ...
|
||||
if (isWidget && !isTemp) {
|
||||
if (isWidget && !isTemp)
|
||||
{
|
||||
folderPath = GetWidgetsFolderPath();
|
||||
}
|
||||
|
||||
@@ -595,13 +667,15 @@ namespace xStorageService.Providers {
|
||||
// if both isTemp and isWidget equals false
|
||||
// which is the default state, retrieve proper folder path
|
||||
// based on file type and return it ...
|
||||
if (!isTemp && !isWidget) {
|
||||
if (!isTemp && !isWidget)
|
||||
{
|
||||
folderPath = GetFileTypeFolderPath(fileType);
|
||||
}
|
||||
|
||||
//
|
||||
// Check folderPath restrict to have Value ...
|
||||
if (folderPath.IsNullOrEmpty ()) {
|
||||
if (folderPath.IsNullOrEmpty())
|
||||
{
|
||||
XException.InvalidData.Throw();
|
||||
}
|
||||
|
||||
@@ -621,7 +695,8 @@ namespace xStorageService.Providers {
|
||||
string fileName,
|
||||
bool isTemp = false,
|
||||
bool isWidget = false
|
||||
) {
|
||||
)
|
||||
{
|
||||
//
|
||||
var folderPath = GetFileFolderPath(
|
||||
fileName,
|
||||
@@ -630,7 +705,8 @@ namespace xStorageService.Providers {
|
||||
);
|
||||
|
||||
//
|
||||
if (folderPath.IsNullOrEmpty ()) {
|
||||
if (folderPath.IsNullOrEmpty())
|
||||
{
|
||||
XException.ActionFailed.Throw();
|
||||
}
|
||||
|
||||
@@ -642,11 +718,17 @@ namespace xStorageService.Providers {
|
||||
/// Save a File into Storage Based on it's Type
|
||||
/// </summary>
|
||||
/// <param name="file"></param>
|
||||
/// <param name="isTemp"></param>
|
||||
/// <param name="isWidget"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<string> SaveFileAsync(
|
||||
IFormFile file,
|
||||
bool isTemp = false,
|
||||
bool isWidget = false) {
|
||||
bool isWidget = false,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
ValidateFile(file);
|
||||
@@ -659,8 +741,13 @@ namespace xStorageService.Providers {
|
||||
|
||||
//
|
||||
// Save File ...
|
||||
using (var stream = new FileStream (fileFullPath, FileMode.Create)) {
|
||||
await file.CopyToAsync (stream);
|
||||
using (var stream = new FileStream(fileFullPath, FileMode.Create))
|
||||
{
|
||||
//
|
||||
await file.CopyToAsync(
|
||||
stream,
|
||||
cancellationToken
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
@@ -674,20 +761,26 @@ namespace xStorageService.Providers {
|
||||
/// Delete a Physical File by Validate it's Exists
|
||||
/// </summary>
|
||||
/// <param name="fileFullPath"></param>
|
||||
/// <param name="forceFileExists"></param>
|
||||
/// <param name="exception"></param>
|
||||
public void DeleteFile(
|
||||
string fileFullPath,
|
||||
bool forceFileExists = true,
|
||||
Exception exception = null) {
|
||||
Exception exception = null
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
if (!forceFileExists &&
|
||||
fileFullPath.IsNullOrEmpty ()) {
|
||||
fileFullPath.IsNullOrEmpty())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Prepare Default Exception ...
|
||||
if (exception == null) {
|
||||
if (exception == null)
|
||||
{
|
||||
exception = XException.NotFound.ToException();
|
||||
}
|
||||
|
||||
@@ -695,7 +788,8 @@ namespace xStorageService.Providers {
|
||||
// Check File Exists ...
|
||||
var isFileExists = File.Exists(fileFullPath);
|
||||
if (forceFileExists &&
|
||||
!isFileExists) {
|
||||
!isFileExists)
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
@@ -703,7 +797,8 @@ namespace xStorageService.Providers {
|
||||
// Handle Prevent Error when forceFileExists false
|
||||
// and File doesn't Exists ...
|
||||
if (!forceFileExists &&
|
||||
!isFileExists) {
|
||||
!isFileExists)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -723,21 +818,25 @@ namespace xStorageService.Providers {
|
||||
ICollection<string> fileFullPaths,
|
||||
bool forceFileExists = true,
|
||||
Exception exception = null
|
||||
) {
|
||||
)
|
||||
{
|
||||
//
|
||||
if (exception.IsNull ()) {
|
||||
if (exception.IsNull())
|
||||
{
|
||||
exception = XException.NotFound.ToException();
|
||||
}
|
||||
|
||||
//
|
||||
var result = fileFullPaths.Where(f => !IsFileExists(GetFileFullPath(f))).ToList();
|
||||
if (forceFileExists && result.HasChild ()) {
|
||||
if (forceFileExists && result.HasChild())
|
||||
{
|
||||
throw exception;
|
||||
}
|
||||
|
||||
//
|
||||
var existsFile = fileFullPaths.Where(f => IsFileExists(f)).ToList();
|
||||
existsFile.ForEach (f => {
|
||||
existsFile.ForEach(f =>
|
||||
{
|
||||
DeleteFile(
|
||||
f,
|
||||
forceFileExists: forceFileExists,
|
||||
@@ -753,18 +852,26 @@ namespace xStorageService.Providers {
|
||||
/// Create Thumbnail based on FileName.Ext
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task CreateThumbnail (string fileName) {
|
||||
public async Task CreateThumbnail(
|
||||
string fileName,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
if (fileName.IsNullOrEmpty ()) {
|
||||
if (fileName.IsNullOrEmpty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
await Task.Run (() => {
|
||||
await Task.Run(() =>
|
||||
{
|
||||
//
|
||||
var fileFullPath = GetFileFullPath(fileName);
|
||||
if (!IsFileExists (fileFullPath)) {
|
||||
if (!IsFileExists(fileFullPath))
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
@@ -779,40 +886,48 @@ namespace xStorageService.Providers {
|
||||
Configuration.ThumbSize,
|
||||
Configuration.ThumbQuality
|
||||
);
|
||||
});
|
||||
}, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create Thumbnail based on XFile model
|
||||
/// </summary>
|
||||
/// <param name="file"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task CreateThumbnail (XFileDto file) {
|
||||
public async Task CreateThumbnail(
|
||||
XFileDto file,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
await CreateThumbnail (file.Name);
|
||||
await CreateThumbnail(
|
||||
file.Name,
|
||||
cancellationToken
|
||||
);
|
||||
}
|
||||
|
||||
// /// <summary>
|
||||
// /// Create Thumbnail based on XFile model
|
||||
// /// </summary>
|
||||
// /// <param name="file"></param>
|
||||
// /// <returns></returns>
|
||||
// public async Task CreateThumbnail (XFile file) {
|
||||
// //
|
||||
// await CreateThumbnail (file.Name);
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// Save and Create a Thumbnail For Profile Image
|
||||
/// </summary>
|
||||
/// <param name="file"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XFileResult> HandleProfileImageSave (IFormFile file) {
|
||||
public async Task<XFileResult> HandleProfileImageSave(
|
||||
IFormFile file,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
ValidateProfileImage(file);
|
||||
|
||||
//
|
||||
var savedFileName = await SaveFileAsync (file, isTemp : false, isWidget : false);
|
||||
var savedFileName = await SaveFileAsync(
|
||||
file,
|
||||
isTemp: false,
|
||||
isWidget: false,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
var thumbFileName = GenerateThumbName(savedFileName);
|
||||
|
||||
//
|
||||
@@ -820,10 +935,14 @@ namespace xStorageService.Providers {
|
||||
var thumbFullPath = GetThumbnailFileFullPath(thumbFileName);
|
||||
|
||||
//
|
||||
await CreateThumbnail (savedFileName);
|
||||
await CreateThumbnail(
|
||||
savedFileName,
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return new XFileResult {
|
||||
return new XFileResult
|
||||
{
|
||||
Name = file.Name,
|
||||
FileName = savedFileName,
|
||||
FilePath = ConvertToProfileImageUrl(savedFileFullPath),
|
||||
@@ -836,8 +955,13 @@ namespace xStorageService.Providers {
|
||||
/// Handle File Upload with Support of Thumbnails ...
|
||||
/// </summary>
|
||||
/// <param name="file"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XFileResult> HandleFileSave (IFormFile file) {
|
||||
public async Task<XFileResult> HandleFileSave(
|
||||
IFormFile file,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
ValidateFile(file);
|
||||
|
||||
@@ -851,7 +975,8 @@ namespace xStorageService.Providers {
|
||||
var thumbFileName = "";
|
||||
var thumbFullPath = "";
|
||||
var fileType = GetFileType(savedFileName);
|
||||
if (fileType.IsImageKind ()) {
|
||||
if (fileType.IsImageKind())
|
||||
{
|
||||
//
|
||||
thumbFileName = GenerateThumbName(savedFileName);
|
||||
thumbFullPath = GetThumbnailFileFullPath(thumbFileName);
|
||||
@@ -862,7 +987,8 @@ namespace xStorageService.Providers {
|
||||
|
||||
//
|
||||
var result =
|
||||
new XFileResult {
|
||||
new XFileResult
|
||||
{
|
||||
Name = file.FileName,
|
||||
FileName = savedFileName,
|
||||
FilePath = ConvertToFileUrl(savedFileFullPath),
|
||||
@@ -878,16 +1004,27 @@ namespace xStorageService.Providers {
|
||||
/// Handle File Upload with Support of Thumbnails ...
|
||||
/// </summary>
|
||||
/// <param name="files"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IEnumerable<XFileResult>> HandleFilesSave (IFormFileCollection files) {
|
||||
public async Task<IEnumerable<XFileResult>> HandleFilesSave(
|
||||
IFormFileCollection files,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
ValidateFiles(files);
|
||||
|
||||
//
|
||||
var result = new Collection<XFileResult>();
|
||||
foreach (var file in files) {
|
||||
foreach (var file in files)
|
||||
{
|
||||
//
|
||||
var savedFileName = await SaveFileAsync (file, isTemp : false, isWidget : false);
|
||||
var savedFileName = await SaveFileAsync(
|
||||
file,
|
||||
isTemp: false,
|
||||
isWidget: false,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
var savedFileFullPath = GetFileFullPath(savedFileName);
|
||||
@@ -896,18 +1033,23 @@ namespace xStorageService.Providers {
|
||||
var thumbFileName = "";
|
||||
var thumbFullPath = "";
|
||||
var fileType = GetFileType(savedFileName);
|
||||
if (fileType.IsImageKind ()) {
|
||||
if (fileType.IsImageKind())
|
||||
{
|
||||
//
|
||||
thumbFileName = GenerateThumbName(savedFileName);
|
||||
thumbFullPath = GetThumbnailFileFullPath(thumbFileName);
|
||||
|
||||
//
|
||||
await CreateThumbnail (savedFileName);
|
||||
await CreateThumbnail(
|
||||
savedFileName,
|
||||
cancellationToken
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
result.Add(
|
||||
new XFileResult {
|
||||
new XFileResult
|
||||
{
|
||||
Name = file.FileName,
|
||||
FileName = savedFileName,
|
||||
FilePath = ConvertToFileUrl(savedFileFullPath),
|
||||
|
||||
+2
-1
@@ -1,7 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<packageSources>
|
||||
<!-- <add key="megan" value="https://hub.megan.ir/nuget/index.json" /> -->
|
||||
<add key="nuget" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
|
||||
<add key="liget" value="https://nuget.saherelmhub.ir/v3/index.json" protocolVersion="3" />
|
||||
<add key="baget" value="https://nuget.saherelmhub.ir/v3/index.json" protocolVersion="3" disableTLSCertificateValidation="true" />
|
||||
</packageSources>
|
||||
</configuration>
|
||||
+19
-6
@@ -2,11 +2,13 @@
|
||||
|
||||
<!-- Runtime Definition -->
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<PackageId>xDashboard.xStorageService</PackageId>
|
||||
<Version>1.0.0</Version>
|
||||
<LangVersion>12.0</LangVersion>
|
||||
<Authors>Hadi Khazaee Asl</Authors>
|
||||
<Company>SaherElm IT Center</Company>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<PackageId>xDashboard.xStorageService</PackageId>
|
||||
|
||||
<Description>
|
||||
provide File Storage/Validating/Handling and etc tools to xDashboard project.
|
||||
</Description>
|
||||
@@ -17,19 +19,30 @@
|
||||
|
||||
<!-- Icon Handling -->
|
||||
<ItemGroup>
|
||||
<None Include="../../Resources/Images/favicon.png" Link="icon.png" Pack="true" PackagePath="\icon.png" />
|
||||
<None Include="../../Resources/Images/favicon.png" Link="icon.png" Pack="true"
|
||||
PackagePath="\icon.png" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- Local Modules -->
|
||||
<ItemGroup>
|
||||
<PackageReference Include="xDashboard.xModels" Version="1.0.0" />
|
||||
<!-- <ProjectReference Include="../xModels/xModels.csproj" /> -->
|
||||
</ItemGroup>
|
||||
|
||||
<!-- Local Dependencies -->
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="../xModels/xModels.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- Dependencies -->
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Magick.NET-Q8-AnyCPU" Version="7.23.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.FileProviders.Physical" Version="5.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.FileProviders.Physical" Version="3.1.32.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- For XML Documentation Support -->
|
||||
<PropertyGroup>
|
||||
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<NoWarn>$(NoWarn);1591</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user