public static void DeleteFile(IFileEntity file, HttpContextBase context) { if (file == null) { return; } try { var fullname = context.Server.MapPath(file.Name); var path = Path.GetDirectoryName(fullname); var filename = Path.GetFileNameWithoutExtension(fullname); var ext = Path.GetExtension(fullname); var mask = string.Format("{0}*{1}", filename, ext); var files = Directory.GetFiles(path, mask); foreach (var f in files) { try { File.Delete(f); } catch { } } } catch { } }
/// <summary> /// Saves the file asynchronous. /// </summary> /// <param name="file">The file.</param> /// <param name="bytes">The bytes.</param> /// <param name="resizeWidth">If the file is an image is the resize width</param> /// <param name="resizeHeight">If the file is an image is the resize height</param> /// <exception cref="System.ArgumentException"> /// The file does not contain a valid id /// or /// The bytes does not have content /// </exception> public void SaveFile(IFileEntity file, byte[] bytes, int resizeWidth = 500, int resizeHeight = 500) { if (file.Id <= 0) { throw new ArgumentException("The file does not contain a valid id"); } else if (bytes.Length == 0) { throw new ArgumentException("The bytes does not have content"); } var fullPath = this.GetPhysicalPath(file); var directory = System.IO.Path.GetDirectoryName(fullPath); if (!System.IO.Directory.Exists(directory)) { System.IO.Directory.CreateDirectory(directory); } if (this.IsImageExtension(fullPath)) { // Resizes the image with the same name this.pictureResizerService.ResizePicture(bytes, fullPath, resizeWidth, resizeHeight, ResizeMode.Max); } else { System.IO.File.WriteAllBytes(fullPath, bytes); } }
/// <summary> /// Gets the size of the file name with. /// </summary> /// <param name="file">The file.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <returns>the file name</returns> private string GetFileNameWithSize(IFileEntity file, int width = 0, int height = 0) { var nameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(file.FileName); var extension = System.IO.Path.GetExtension(file.FileName); if (width != 0 && height != 0) { return($"{file.Id}_{nameWithoutExtension}_{width}x{height}{extension}"); } else { return($"{file.Id}{extension}"); } }
public Task <object> Add(IFileEntity entity) { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } return(Task.Run <object>(() => { var id = this.guidProvider.NewGuid(); var now = this.dateTimeProvider.Now; var dbentity = new File { Id = id, DateCreated = now, DateModified = now, ContentLength = entity.ContentLength, ContentType = entity.ContentType, CreatedByUser = entity.CreatedByUser, Description = entity.Description, FileExtension = entity.FileExtension, FileName = entity.FileName, FullName = entity.FullName, ModifiedByUser = entity.ModifiedByUser, OriginalContentLength = entity.ContentLength, OriginalContentType = entity.ContentType, OriginalFileExtension = entity.FileExtension, OriginalFileName = entity.FileName }; var entry = this.GetEntry(dbentity); if (entry.State != EntityState.Detached) { entry.State = EntityState.Added; return dbentity; } else { return this.DbSet.Add(dbentity); } })); }
/// <summary> /// Gets the full path. /// </summary> /// <param name="file">The file.</param> /// <param name="contentUrlFunction">The content URL function.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="forceResize">forces the resize</param> /// <returns> /// the path /// </returns> public string GetFullPath(IFileEntity file, Func <string, string> contentUrlFunction = null, int width = 0, int height = 0, bool forceResize = false) { var fileName = $"/img/content/{this.GetFolderName(file)}/{this.GetFileNameWithSize(file, width, height)}"; var physicalPath = string.Concat(this.hostingEnvironment.WebRootPath, fileName); if (forceResize && !System.IO.File.Exists(physicalPath)) { var originalPath = this.GetPhysicalPath(file); this.pictureResizerService.ResizePicture(physicalPath, originalPath, width, height, ResizeMode.Crop); } if (contentUrlFunction != null) { return(contentUrlFunction(fileName)); } else { return(fileName); } }
public async Task <IActionResult> UploadFile(IFormFile file, string invoiceId) { var isValid = await ValidateRequest(); if ((isValid as OkResult)?.StatusCode != Ok().StatusCode) { return(isValid); } if (string.IsNullOrEmpty(invoiceId)) { return(BadRequest()); } long size = file.Length; if (size > _payApiSettings.MaxUploadFileSize) { return(BadRequest("Incorrect File Size")); } var fileInfo = new IFileEntity { FileId = Guid.NewGuid().ToString(), FileName = file.FileName, FileSize = (int)size, InvoiceId = invoiceId }; using (var ms = new MemoryStream()) { await file.CopyToAsync(ms); fileInfo.FileBodyBase64 = Convert.ToBase64String(ms.ToArray()); } await _invoiceService.ApiInvoicesUploadFilePostWithHttpMessagesAsync(fileInfo); return(Ok()); }
/// <summary> /// Gets the name of the folder. /// </summary> /// <param name="file">The file.</param> /// <param name="filesByFolder">The files by folder.</param> /// <returns> /// the folder where the file is /// </returns> public string GetFolderName(IFileEntity file, int filesByFolder = 50) { var folder = Math.Ceiling((decimal)file.Id / filesByFolder); return(folder.ToString("000000")); }
/// <summary> /// Gets the physical path. /// </summary> /// <param name="file">The file.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <returns> /// the physical path /// </returns> public string GetPhysicalPath(IFileEntity file, int width = 0, int height = 0) { var relativePath = this.GetFullPath(file, null, width, height); return(string.Concat(this.hostingEnvironment.WebRootPath, relativePath)); }
public static string _FileName(this IFileEntity obj, string extension) { return(obj._Folder + obj._DisplayName + "." + extension); }
public static IHtmlString Image(this HtmlHelper helper, IFileEntity file, object attrs, params string[] parameters) { var relativePath = file == null ? null : file.Name; return(helper.Image(relativePath, attrs, parameters)); }
public static string Image(this UrlHelper helper, IFileEntity file, params string[] parameters) { var relativePath = file == null ? null : file.Name; return(helper.Image(relativePath, parameters)); }
public System.Web.WebPages.HelperResult RenderScript(UploadFileSettings upload, IFileEntity file) { #line default #line hidden return(new System.Web.WebPages.HelperResult(__razor_helper_writer => { #line 74 "..\..\Areas\Admin\Views\Shared\EditorTemplates\ozi_File.cshtml" #line default #line hidden WriteLiteralTo(__razor_helper_writer, " "); WriteLiteralTo(__razor_helper_writer, "\r\n files[\""); #line 76 "..\..\Areas\Admin\Views\Shared\EditorTemplates\ozi_File.cshtml" WriteTo(__razor_helper_writer, upload.Name); #line default #line hidden WriteLiteralTo(__razor_helper_writer, "_array\"].files.push(new FileItem({ Id: "); #line 76 "..\..\Areas\Admin\Views\Shared\EditorTemplates\ozi_File.cshtml" WriteTo(__razor_helper_writer, file.Id); #line default #line hidden WriteLiteralTo(__razor_helper_writer, ", Url: \""); #line 76 "..\..\Areas\Admin\Views\Shared\EditorTemplates\ozi_File.cshtml" WriteTo(__razor_helper_writer, Url.Content(file.Name)); #line default #line hidden WriteLiteralTo(__razor_helper_writer, "\", SourceName: \""); #line 76 "..\..\Areas\Admin\Views\Shared\EditorTemplates\ozi_File.cshtml" WriteTo(__razor_helper_writer, file.SourceName); #line default #line hidden WriteLiteralTo(__razor_helper_writer, "\", Alt: \""); #line 76 "..\..\Areas\Admin\Views\Shared\EditorTemplates\ozi_File.cshtml" WriteTo(__razor_helper_writer, file.Alt); #line default #line hidden WriteLiteralTo(__razor_helper_writer, "\", Title: \""); #line 76 "..\..\Areas\Admin\Views\Shared\EditorTemplates\ozi_File.cshtml" WriteTo(__razor_helper_writer, file.Title); #line default #line hidden WriteLiteralTo(__razor_helper_writer, "\", Description: \""); #line 76 "..\..\Areas\Admin\Views\Shared\EditorTemplates\ozi_File.cshtml" WriteTo(__razor_helper_writer, file.Description); #line default #line hidden WriteLiteralTo(__razor_helper_writer, "\", Date: \""); #line 76 "..\..\Areas\Admin\Views\Shared\EditorTemplates\ozi_File.cshtml" WriteTo(__razor_helper_writer, file.Date); #line default #line hidden WriteLiteralTo(__razor_helper_writer, "\", Visibility: "); #line 76 "..\..\Areas\Admin\Views\Shared\EditorTemplates\ozi_File.cshtml" WriteTo(__razor_helper_writer, file.Visibility.ToString().ToLower()); #line default #line hidden WriteLiteralTo(__razor_helper_writer, " }));\r\n "); WriteLiteralTo(__razor_helper_writer, "\r\n"); #line 78 "..\..\Areas\Admin\Views\Shared\EditorTemplates\ozi_File.cshtml" #line default #line hidden })); #line 78 "..\..\Areas\Admin\Views\Shared\EditorTemplates\ozi_File.cshtml" }
public Task <object> Update(IFileEntity entity) { throw new NotImplementedException(); }