/// <summary> /// Delete a file on file system /// </summary> /// <param name="file">file</param> protected virtual void DeleteFileFromFileSystem(File file) { if (file == null) { throw new ArgumentNullException(nameof(file)); } var filename = $"{file.StoredName}.{file.LastPart}"; var filePath = GetFileLocalPath(filename); _fileProvider.DeleteFile(filePath); }
/// <summary> /// Gets the loaded file binary depending on file storage settings /// </summary> /// <param name="file">file</param> /// <param name="fromDb">Load from database; otherwise, from file system</param> /// <returns>file binary</returns> protected virtual byte[] LoadFileBinary(File file, bool fromDb) { if (file == null) { throw new ArgumentNullException(nameof(file)); } var result = fromDb ? GetFileBinaryByFileId(file.Id)?.BinaryData ?? Array.Empty <byte>() : LoadFileFromFileSystem(file.Id); return(result); }
/// <summary> /// Deletes a file /// </summary> /// <param name="file">file</param> public virtual void DeleteFile(File file) { if (file == null) { throw new ArgumentNullException(nameof(file)); } //delete from file system if (!StoreInDb) { DeleteFileFromFileSystem(file); } //delete from database _fileRepository.Delete(file); //event notification // _eventPublisher.EntityDeleted(file); }
/// <summary> /// Updates the file /// </summary> /// <param name="file">The file to update</param> /// <returns>file</returns> public virtual File UpdateFile(File file) { if (file == null) { return(null); } _fileRepository.Update(file); UpdateFileBinary(file, StoreInDb ? GetFileBinaryByFileId(file.Id).BinaryData : Array.Empty <byte>()); if (!StoreInDb) { SaveFileInFileSystem(file.Id, GetFileBinaryByFileId(file.Id).BinaryData, file.MimeType); } //event notification //_eventPublisher.EntityUpdated(file); return(file); }
/// <summary> /// Updates the file binary data /// </summary> /// <param name="file">The file object</param> /// <param name="binaryData">The file binary data</param> /// <returns>file binary</returns> protected virtual FileBinary UpdateFileBinary(File file, byte[] binaryData) { if (file == null) { throw new ArgumentNullException(nameof(file)); } var fileBinary = GetFileBinaryByFileId(file.Id); var isNew = fileBinary == null; if (isNew) { fileBinary = new FileBinary { FileId = file.Id } } ; fileBinary.BinaryData = binaryData; if (isNew) { _fileBinaryRepository.Add(fileBinary); //event notification // _eventPublisher.EntityInserted(fileBinary); } else { _fileBinaryRepository.Update(fileBinary); //event notification //_eventPublisher.EntityUpdated(fileBinary); } return(fileBinary); }
private void UploadFiles(ICollection <IFormFile> httpPostedFile, string newsItemId) { foreach (var file in httpPostedFile) { var fileBinary = _downloadService.GetDownloadBits(file); var fileName = file.FileName; //remove path (passed in IE) fileName = _fileProvider.GetFileNameWithoutExtension(fileName).Replace('.', '_'); var contentType = file.ContentType; var fileExtension = _fileProvider.GetFileExtension(file.FileName); if (!string.IsNullOrEmpty(fileExtension)) { fileExtension = fileExtension.ToLowerInvariant(); } var download = new File { ContentType = contentType, Filename = _fileProvider.GetFileNameWithoutExtension(fileName), Extension = fileExtension, NewsItemId = newsItemId, Name = file.Name }; if (!StoreInDb) //if file saving into filesystem { var pathToFile = _downloadService.SaveFileInFileSystem(fileBinary, fileBinary.GetHashCode().ToString()); download.DownloadUrl = _fileProvider.GetFileName(pathToFile); } else { download.DownloadUrl = string.Empty; download.DownloadBinary = fileBinary; } _downloadService.InsertDownload(download); } }
/// <summary> /// Inserts a file /// </summary> /// <param name="fileBinary">The file binary</param> /// <param name="mimeType">The file MIME type</param> /// <param name="fileName"></param> /// <returns>file</returns> public virtual File InsertFile(byte[] fileBinary, string mimeType, string fileName, string lastPart) { var file = new File() { MimeType = mimeType, OriginalName = fileName, LastPart = lastPart, StoredName = fileBinary.GetHashCode().ToString("0000"), Md5Hash = GetMd5(fileBinary) }; _fileRepository.Add(file); UpdateFileBinary(file, StoreInDb ? fileBinary : Array.Empty <byte>()); if (!StoreInDb) { SaveFileInFileSystem(file.Id, fileBinary, mimeType); } //event notification // _eventPublisher.EntityInserted(file); return(file); }
/// <summary> /// Gets the loaded picture binary depending on picture storage settings /// </summary> /// <param name="file">file</param> /// <returns>Picture binary</returns> public virtual byte[] LoadFileBinary(File file) { return(LoadFileBinary(file, StoreInDb)); }