/// <summary> /// Inserts a file /// </summary> /// <returns>File</returns> public virtual File InsertFile(byte[] fileBinary, string fileName, string fileExtension, string seoFilename, bool isNew = true) { fileName = CommonHelper.EnsureNotNull(fileName); fileName = CommonHelper.EnsureMaximumLength(fileName, 20); seoFilename = CommonHelper.EnsureMaximumLength(seoFilename, 100); var file = new File { Name = fileName, Extension = fileExtension, SeoFilename = seoFilename, IsNew = isNew, }; _fileRepository.Insert(file); //保存文件 SaveFileInSystem(file.Id, fileBinary, fileExtension); //event notification _eventPublisher.EntityInserted(file); return(file); }
/// <summary> /// Gets the loaded file binary depending on file storage settings /// </summary> /// <param name="file">File</param> /// <returns>File binary</returns> public virtual byte[] LoadFileBinary(File file) { if (file == null) { throw new ArgumentNullException("file"); } var result = LoadFileFromLocation(file.Id, file.Extension); return(result); }
/// <summary> /// Delete a file on file system /// </summary> /// <param name="file">File</param> protected virtual void DeleteFileOnFileSystem(File file) { if (file == null) { throw new ArgumentNullException("file"); } string fileName = string.Format("{0}_0{1}", file.Id.ToString("0000000"), file.Extension); string filePath = GetFileLocalPath(fileName); if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } }
/// <summary> /// Deletes a file /// </summary> /// <param name="file">File</param> public virtual void DeleteFile(File file) { if (file == null) { throw new ArgumentNullException("file"); } //delete from file system DeleteFileOnFileSystem(file); //delete from database _fileRepository.Delete(file); //event notification _eventPublisher.EntityDeleted(file); }
/// <summary> /// Get a file URL /// </summary> /// <param name="file">File instance</param> /// <returns>File URL</returns> public virtual string GetFileUrl(File file) { var url = _webHelper.GetStoreLocation() + "content/files/"; byte[] fileBinary = null; if (file != null) { fileBinary = LoadFileBinary(file); } if (file == null || fileBinary == null || fileBinary.Length == 0) { string fileName = string.Format("{0}_0{1}", file.Id.ToString("0000000"), file.Extension); url += fileName; return(url); } return(url); }