private string GenerateThumbnail(NewFileModel newFile, string uri) { var subFolder = @"files\thumbnails"; string path = Path.Combine(Env.ContentRootPath, subFolder); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var fileName = $"{Context.Files.Count()}_{newFile.File.FileName}"; string filePath = Path.Combine(path, fileName); ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.CreateNoWindow = false; startInfo.UseShellExecute = false; startInfo.FileName = Path.Combine(Env.ContentRootPath, @"files\thumbnails\CiffCaffParser.exe"); startInfo.WindowStyle = ProcessWindowStyle.Normal; startInfo.Arguments = uri + " " + filePath; try { using (Process exeProcess = Process.Start(startInfo)) { exeProcess.WaitForExit(); } } catch { filePath = Path.Combine(path, "no-thumbnail"); } return(filePath + ".bmp"); }
public async Task <IActionResult> UploadImage([FromForm] NewFileModel newFile) { var(canCreated, error) = await fileManager.CreateNewFileAsync(newFile.Folder, newFile.Name, newFile.File); if (canCreated) { return(Created($"folders/{newFile.Folder}/image/{newFile.Name}", null)); } return(Conflict(new { Error = error })); }
public async Task <NewFileModel> UploadFile(NewFileModel newFile) { var caffUri = SaveCaffFile(newFile); var thumbnailUri = GenerateThumbnail(newFile, caffUri); var newEntity = new CaffFile { CAFFUri = caffUri, ThumbnailUri = thumbnailUri, Title = newFile.Title, Description = newFile.Description, OwnerId = UserManager.GetUserId() }; Context.Files.Add(newEntity); await Context.SaveChangesAsync(); return(newFile); }
// https://stackoverflow.com/a/39394266 private string SaveCaffFile(NewFileModel newFile) { var subFolder = @"files\caffs"; string path = Path.Combine(Env.ContentRootPath, subFolder); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var fileName = $"{Context.Files.Count()}_{newFile.Title}.caff"; string filePath = Path.Combine(path, fileName); byte[] fileBytes; using (var ms = new MemoryStream()) { newFile.File.CopyTo(ms); fileBytes = ms.ToArray(); File.WriteAllBytes(filePath, fileBytes); } return(filePath); }
public async Task UploadFile([FromForm] NewFileModel newFile) => await CaffFileService.UploadFile(newFile);