public MultipleImageUploadBehavior(ITextLocalizer localizer, IUploadStorage storage, IExceptionLogger logger = null) { this.localizer = localizer; this.storage = storage; this.logger = logger; }
public static CopyTemporaryFileResult CopyTemporaryFile(this IUploadStorage uploadStorage, CopyTemporaryFileOptions options) { if (uploadStorage is null) { throw new ArgumentNullException(nameof(uploadStorage)); } long size = uploadStorage.GetFileSize(options.TemporaryFile); string path = PathHelper.ToUrl(UploadFormatting.FormatFilename(options)); path = uploadStorage.CopyFrom(uploadStorage, options.TemporaryFile, path, autoRename: true); bool hasThumbnail = uploadStorage.FileExists(UploadPathHelper.GetThumbnailName(options.TemporaryFile)); var result = new CopyTemporaryFileResult() { FileSize = size, Path = path, OriginalName = options.OriginalName, HasThumbnail = hasThumbnail }; options.FilesToDelete?.RegisterNewFile(path); options.FilesToDelete?.RegisterOldFile(options.TemporaryFile); return(result); }
public FileController(IUploadStorage uploadStorage, ITextLocalizer localizer) { UploadStorage = uploadStorage ?? throw new ArgumentNullException(nameof(uploadStorage)); Localizer = localizer ?? throw new ArgumentNullException(nameof(localizer)); }
public CombinedUploadStorage(IUploadStorage mainStorage, IUploadStorage subStorage, string subPrefix) { this.mainStorage = mainStorage ?? throw new ArgumentNullException(nameof(mainStorage)); this.subStorage = subStorage ?? throw new ArgumentNullException(nameof(subStorage)); this.subPrefix = PathHelper.ToUrl(subPrefix ?? throw new ArgumentNullException(nameof(subPrefix))); if (!this.subPrefix.EndsWith("/")) { this.subPrefix += "/"; } }
public static string GetThumbnailUrl(this IUploadStorage uploadStorage, string path) { if (string.IsNullOrEmpty(path)) { return(null); } var thumb = UploadPathHelper.GetThumbnailName(path); return(uploadStorage.GetFileUrl(thumb)); }
public static byte[] ReadAllFileBytes(this IUploadStorage uploadStorage, string path) { if (uploadStorage is null) { throw new ArgumentNullException(nameof(uploadStorage)); } using var ms = new MemoryStream(); using (var fs = uploadStorage.OpenFile(path)) fs.CopyTo(ms); return(ms.ToArray()); }
public static void SetOriginalName(this IUploadStorage uploadStorage, string path, string originalName) { if (uploadStorage is null) { throw new ArgumentNullException(nameof(uploadStorage)); } var metadata = new Dictionary <string, string>() { [FileMetadataKeys.OriginalName] = originalName }; uploadStorage.SetFileMetadata(path, metadata, overwriteAll: false); }
public static string GetOriginalName(this IUploadStorage uploadStorage, string path) { if (uploadStorage is null) { throw new ArgumentNullException(nameof(uploadStorage)); } var metadata = uploadStorage.GetFileMetadata(path); if (metadata != null && metadata.TryGetValue(FileMetadataKeys.OriginalName, out string originalName)) { return(originalName); } return(null); }
public string CopyFrom(IUploadStorage store, string sourcePath, string targetPath, bool autoRename) { if (string.IsNullOrEmpty(sourcePath)) { throw new ArgumentNullException(sourcePath); } if (IsInternalFile(sourcePath)) { throw new ArgumentOutOfRangeException(nameof(sourcePath)); } if (string.IsNullOrEmpty(targetPath)) { throw new ArgumentNullException(nameof(targetPath)); } if (IsInternalFile(targetPath)) { throw new ArgumentOutOfRangeException(nameof(targetPath)); } var newFiles = new List <string>(); using var source = store.OpenFile(sourcePath); targetPath = WriteFile(targetPath, source, autoRename); newFiles.Add(targetPath); try { var sourceBasePath = Path.ChangeExtension(sourcePath, null); var sourceBaseName = Path.GetFileNameWithoutExtension(sourceBasePath); var targetBasePath = Path.ChangeExtension(targetPath, null); var sourceDir = Path.GetDirectoryName(sourcePath); foreach (var f in store.GetFiles(sourceDir, sourceBaseName + "_t*.jpg")) { string thumbSuffix = Path.GetFileName(f)[sourceBaseName.Length..];
public UploadProcessor(IUploadStorage storage, IExceptionLogger logger = null) { ThumbBackColor = Color.Empty; this.storage = storage ?? throw new ArgumentNullException(nameof(storage)); Logger = logger; }
public ExcelImportResponse ExcelImport(IUnitOfWork uow, ExcelImportRequest request, [FromServices] IUploadStorage uploadStorage) { if (request is null) { throw new ArgumentNullException(nameof(request)); } if (string.IsNullOrWhiteSpace(request.FileName)) { throw new ArgumentNullException(nameof(request.FileName)); } if (uploadStorage is null) { throw new ArgumentNullException(nameof(uploadStorage)); } UploadPathHelper.CheckFileNameSecurity(request.FileName); if (!request.FileName.StartsWith("temporary/")) { throw new ArgumentOutOfRangeException(nameof(request.FileName)); } ExcelPackage ep = new ExcelPackage(); using (var fs = uploadStorage.OpenFile(request.FileName)) ep.Load(fs); var p = ProductRow.Fields; var s = SupplierRow.Fields; var c = CategoryRow.Fields; var response = new ExcelImportResponse(); response.ErrorList = new List <string>(); var worksheet = ep.Workbook.Worksheets[0]; for (var row = 2; row <= worksheet.Dimension.End.Row; row++) { try { var productName = Convert.ToString(worksheet.Cells[row, 1].Value ?? ""); if (productName.IsTrimmedEmpty()) { continue; } var product = uow.Connection.TryFirst <ProductRow>(q => q .Select(p.ProductID) .Where(p.ProductName == productName)); if (product == null) { product = new ProductRow { ProductName = productName } } ; else { // avoid assignment errors product.TrackWithChecks = false; } var supplierName = Convert.ToString(worksheet.Cells[row, 2].Value ?? ""); if (!string.IsNullOrWhiteSpace(supplierName)) { var supplier = uow.Connection.TryFirst <SupplierRow>(q => q .Select(s.SupplierID) .Where(s.CompanyName == supplierName)); if (supplier == null) { response.ErrorList.Add("Error On Row " + row + ": Supplier with name '" + supplierName + "' is not found!"); continue; } product.SupplierID = supplier.SupplierID.Value; } else { product.SupplierID = null; } var categoryName = Convert.ToString(worksheet.Cells[row, 3].Value ?? ""); if (!string.IsNullOrWhiteSpace(categoryName)) { var category = uow.Connection.TryFirst <CategoryRow>(q => q .Select(c.CategoryID) .Where(c.CategoryName == categoryName)); if (category == null) { response.ErrorList.Add("Error On Row " + row + ": Category with name '" + categoryName + "' is not found!"); continue; } product.CategoryID = category.CategoryID.Value; } else { product.CategoryID = null; } product.QuantityPerUnit = Convert.ToString(worksheet.Cells[row, 4].Value ?? ""); product.UnitPrice = Convert.ToDecimal(worksheet.Cells[row, 5].Value ?? 0); product.UnitsInStock = Convert.ToInt16(worksheet.Cells[row, 6].Value ?? 0); product.UnitsOnOrder = Convert.ToInt16(worksheet.Cells[row, 7].Value ?? 0); product.ReorderLevel = Convert.ToInt16(worksheet.Cells[row, 8].Value ?? 0); if (product.ProductID == null) { new ProductRepository(Context).Create(uow, new SaveRequest <MyRow> { Entity = product }); response.Inserted = response.Inserted + 1; } else { new ProductRepository(Context).Update(uow, new SaveRequest <MyRow> { Entity = product, EntityId = product.ProductID.Value }); response.Updated = response.Updated + 1; } } catch (Exception ex) { response.ErrorList.Add("Exception on Row " + row + ": " + ex.Message); } } return(response); }
public MultipleImageUploadBehavior(ITextLocalizer localizer, IUploadStorage storage) { this.localizer = localizer; this.storage = storage; }
public ImageUploadBehavior(IUploadStorage storage, ITextLocalizer localizer) { this.storage = storage; this.localizer = localizer; }
public FilesToDelete(IUploadStorage storage) { OldFiles = new List <string>(); this.storage = storage ?? throw new ArgumentNullException(nameof(storage)); }