public FileModel CreateMaterial(NewMaterialDTO data) { string name = data.FileName; string category = data.Category; ulong size = (ulong)data.fileBlob.Length; int version = 1; MaterialModel material = new MaterialModel(); material.Name = name; material.Category = category; if (_context.Materials.Count() > 0) { material.Id = _context.Materials.Max(f => f.Id) + 1; } else { material.Id = 1; } // materials.Add(material); _context.Add(material); FileModel file = new FileModel(); file.MaterialId = material.Id; file.Version = version; file.Size = size; file.Id = _context.Files.Count() > 0 ? _context.Files.Max(f => f.Id) + 1 : 1; file.Path = _fileService.UploadFile(material, file, data.fileBlob); file.UploadTime = DateTime.Now; _context.Add(file); _context.SaveChanges(); return(file); }
public byte[] DownloadFile(int id) { FileModel file = _context.Files.Where(f => f.Id == id).FirstOrDefault(); return(System.IO.File.ReadAllBytes(@$ "{file.Path}")); //Stream stream = System.IO.File.OpenRead(@$"{file.Path}"); //return File(stream, "application/octet-stream", file.Name); }
public string UploadFile(MaterialModel material, FileModel fileModel, byte[] file) { string path = $"Files\\{material.Id}-{fileModel.Id}"; File.WriteAllBytes(path, file); /*using (var fileStream = new FileStream(path, FileMode.Create)) * { * file.CopyToAsync(fileStream); * }*/ return(path); }
public FileModel UpdateMaterial(UpdateMaterialDTO data) { MaterialModel material = _context.Materials.Where(m => m.Id == data.MaterialId).FirstOrDefault(); FileModel file = new FileModel(); file.MaterialId = material.Id; file.Version = _context.Files.Where(f => f.MaterialId == material.Id).Max(f => f.Version) + 1; file.Size = (ulong)data.fileBlob.Length; file.Id = _context.Files.Count() > 0 ? _context.Files.Max(f => f.Id) + 1 : 1; file.Path = _fileService.UploadFile(material, file, data.fileBlob); file.UploadTime = DateTime.Now; _context.Add(file); _context.SaveChanges(); return(file); }
public byte[] DownloadMaterial(int id, int version) { FileModel file = _context.Files.Where(f => f.MaterialId == id && f.Version == version).FirstOrDefault(); return(_fileService.DownloadFile(file.Id)); }