public async Task <HttpResponseMessage> CommitChunks([Microsoft.AspNetCore.Mvc.FromBody] DzCommit model) { HttpResponseMessage response = new HttpResponseMessage { StatusCode = HttpStatusCode.OK }; try { BackgroundJob.Enqueue <BackgroundJobHelper>(x => x.CommitFileChunks(model)); } catch (Exception ex) { HelperFunctions.Log(_context, PublicEnums.LogLevel.LEVEL_EXCEPTION, "Controllers.ApiCourseManagementController.CommitChunks", ex.Message, User, ex); response.StatusCode = HttpStatusCode.InternalServerError; response.Content = new StringContent(string.Format("Error merging chunked upload: {0}", ex.Message)); } return(response); }
public HttpResponseMessage DeleteCanceledChunks([Microsoft.AspNetCore.Mvc.FromBody] DzCommit model) { HttpResponseMessage response = new HttpResponseMessage { StatusCode = HttpStatusCode.OK }; try { var chunkDirBasePath = Path.Combine(_env.ContentRootPath, "App_Data"); var path = string.Format(@"{0}\{1}", chunkDirBasePath, model.dzIdentifier); BackgroundJob.Enqueue <BackgroundJobHelper>(x => x.DeleteFolder(path)); } catch (Exception ex) { HelperFunctions.Log(_context, PublicEnums.LogLevel.LEVEL_EXCEPTION, "Controllers.ApiFileShareController.DeleteCanceledChunks", ex.Message, User, ex); response.StatusCode = HttpStatusCode.InternalServerError; response.Content = new StringContent(string.Format("Error deleting canceled chunks: {0}", ex.Message)); } return(response); }
public async Task CommitFileChunks(DzCommit model) { string path = ""; try { var chunkDirBasePath = Path.Combine(_env.ContentRootPath, "App_Data"); path = string.Format(@"{0}\{1}", chunkDirBasePath, model.dzIdentifier); var dest = Path.Combine(path, HttpUtility.UrlDecode(model.fileName)); FileInfo info = null; // Get all files in directory and combine in filestream var files = Directory.EnumerateFiles(path).Where(s => !s.Equals(dest)).OrderBy(s => s); // Check that the number of chunks is as expected if (files.Count() != model.totalChunks) { throw new Exception(string.Format("Total number of chunks: {0}. Expected: {1}!", files.Count(), model.totalChunks)); } // Merge chunks into one file using (var fStream = new FileStream(dest, FileMode.Create)) { foreach (var file in files) { using (var sourceStream = System.IO.File.OpenRead(file)) { sourceStream.CopyTo(fStream); } } fStream.Flush(); } // Check that merged file length is as expected. info = new FileInfo(dest); if (info != null) { if (info.Length == model.expectedBytes) { if (model.folderID != Guid.Empty) { var uploadDirectory = _context.FolderDirectories.FirstOrDefault(x => x.FolderID == model.folderID && x.IsUploadDirectory == true); if (uploadDirectory != null) { if (uploadDirectory.RequireCredentials) { NetworkCredential networkCredential = new NetworkCredential(uploadDirectory.Username, uploadDirectory.Password); CredentialCache theNetcache = new CredentialCache(); theNetcache.Add((new Uri(uploadDirectory.FolderPath)), "Basic", networkCredential); } var finaldest = Path.Combine(uploadDirectory.FolderPath, HttpUtility.UrlDecode(model.fileName)); if (model.parentFolderDirectoryFileID != null && _context.FolderDirectoryFiles.Any(x => x.FolderDirectoryFileID == model.parentFolderDirectoryFileID)) { var folderDirectoryFiles = _context.FolderDirectoryFiles.First(x => x.FolderDirectoryFileID == model.parentFolderDirectoryFileID); finaldest = Path.Combine(folderDirectoryFiles.FullPath, HttpUtility.UrlDecode(model.fileName)); } if (!Directory.Exists(uploadDirectory.FolderPath)) { Directory.CreateDirectory(uploadDirectory.FolderPath); } System.IO.File.Move(dest, finaldest); BackgroundJob.Enqueue <BackgroundJobHelper>(x => x.DeleteFolder(path)); BackgroundJob.Enqueue <BackgroundJobHelper>(x => x.subRescanDirectories(uploadDirectory.FolderDirectoryID, model.parentFolderDirectoryFileID)); } } } else { throw new Exception(string.Format("Total file size: {0}. Expected: {1}!", info.Length, model.expectedBytes)); } } else { throw new Exception("Chunks failed to merge and file not saved!"); } } catch (HttpResponseException ex) { HelperFunctions.Log(_context, PublicEnums.LogLevel.LEVEL_EXCEPTION, "Controllers.ApiCourseManagementController.CommitChunks", ex.Message, null, ex); } catch (Exception ex) { HelperFunctions.Log(_context, PublicEnums.LogLevel.LEVEL_EXCEPTION, "Controllers.ApiCourseManagementController.CommitChunks", ex.Message, null, ex); } finally { BackgroundJob.Enqueue <BackgroundJobHelper>(x => x.DeleteFolder(path)); } }