/// <summary> /// Creates the specified file. /// </summary> /// <param name="file">The file.</param> /// <param name="data">The data.</param> /// <returns></returns> public Entities.File Create(Entities.File file, byte[] data) { var id = Guid.NewGuid(); var subFolder = (file.FileType.ToLower() == "image" ? "img/" : "doc/") + (file.AccountId.HasValue ? file.AccountId.ToString() : "common"); var fullFolder = Path.Combine(_path, subFolder); var extName = Path.GetExtension(file.FileName)?.ToLower(); var filePath = Path.Combine(subFolder, id + extName); var fullFilePath = Path.Combine(fullFolder, id + extName); if (!Directory.Exists(fullFolder)) { Directory.CreateDirectory(fullFolder); } using (var stream = File.Create(fullFilePath)) { stream.Write(data, 0, data.Length); stream.Flush(); stream.Close(); } // Data access component declarations. var fileDAC = new FileDAC(); // Step 1 - Calling Create on FileDAC. file.Id = id; file.FilePath = filePath; file.MimeType = FileContentType.GetMimeType(extName); fileDAC.Create(file); return(file); }
/// <summary> /// Copy a file into the same folder /// </summary> /// <param name="id"></param> /// <returns></returns> public File CopyInFolder(Guid id) { File fileNew = null; var fileDAC = new FileDAC(); var file = fileDAC.SelectById(id); if (file != null) { var toFileId = Guid.NewGuid(); var fullPath = Path.Combine(_path, file.FilePath); var dirPath = Path.GetDirectoryName(file.FilePath); var extName = Path.GetExtension(file.FileName)?.ToLower(); var newFileName = toFileId + extName; var newFullPath = Path.Combine(_path, dirPath ?? throw new InvalidOperationException(), newFileName); System.IO.File.Copy(fullPath, newFullPath); file.Id = toFileId; file.FilePath = Path.Combine(dirPath, newFileName); file.Timestamp = DateTime.Now; fileDAC.Create(file); fileNew = file; fileNew.Id = toFileId; } return(fileNew); }
public Guid[] CreateWithThumbnail(File file, byte[] data) { var originId = Guid.NewGuid(); var subFolder = CreateSubFolder(file.FileType); var fullFolder = Path.Combine(_path, subFolder); var extName = Path.GetExtension(file.FileName)?.ToLower(); var originFilePath = Path.Combine(subFolder, originId + extName); var originfullFilePath = Path.Combine(fullFolder, originId + extName); if (!Directory.Exists(fullFolder)) { Directory.CreateDirectory(fullFolder); } using (var stream = System.IO.File.Create(originfullFilePath)) { stream.Write(data, 0, data.Length); stream.Flush(); stream.Close(); } var compressFile = Compress(data, file); var fileDAC = new FileDAC(); file.Id = originId; file.FilePath = originFilePath; file.MimeType = FileContentType.GetMimeType(extName); file.Md5 = GetMD5HashFromByte(data); using (var scope = new TransactionScope(TransactionScopeOption.Required, new TimeSpan(0, 0, 1, 30))) { fileDAC.Create(file); fileDAC.Create(compressFile); scope.Complete(); } return(new Guid[] { file.Id, compressFile.Id }); }
/// <summary> /// Creates the specified file. /// </summary> /// <param name="file">The file.</param> /// <param name="data">The data.</param> /// <returns></returns> public File Create(File file, byte[] data) { var fileMd5 = GetMD5HashFromByte(data); var fileDAC = new FileDAC(); var oldFile = fileDAC.SelectByMd5(fileMd5); var id = Guid.NewGuid(); var subFolder = CreateSubFolder(file.FileType); var fullFolder = Path.Combine(_path, subFolder); var extName = Path.GetExtension(file.FileName)?.ToLower(); var fullFilePath = Path.Combine(fullFolder, id + extName); if (!Directory.Exists(fullFolder)) { Directory.CreateDirectory(fullFolder); } if (oldFile == null) { file.FilePath = Path.Combine(subFolder, id + extName); using (var stream = System.IO.File.Create(fullFilePath)) { stream.Write(data, 0, data.Length); stream.Flush(); stream.Close(); } } else { file.FilePath = oldFile.FilePath; } file.Id = id; file.MimeType = FileContentType.GetMimeType(extName); file.Md5 = fileMd5; fileDAC.Create(file); return(file); }