/// <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); }
public Entities.File GetByMd5(string md5) { var fileDAC = new FileDAC(); return(fileDAC.SelectByMd5(md5)); }