public IActionResult UploadMultiple(UploadType type, bool isRelativeRequested, [FromForm] MultipleFileUploadModel model) { HttpContext.Request.Headers.TryGetValue("announcementId", out var announcementIdValue); HttpContext.Request.Headers.TryGetValue("announcementPhotoType", out var announcementPhotoTypeValue); int.TryParse(announcementIdValue.FirstOrDefault(), out var announcementId); Enum.TryParse(announcementPhotoTypeValue.FirstOrDefault(), out AttachmentType announcementPhotoType); return(MakeActionCallWithModel(() => _service.MultipleFileUploader(model, type, announcementPhotoType, isRelativeRequested, announcementId), model)); }
public bool MultipleFileUploader(MultipleFileUploadModel model, UploadType type, AttachmentType announcementPhotoType, bool isRelativeRequested, int announcementId) { Parallel.ForEach(model.Files, async file => { var dest = file.FileName.GetDestFileName(); dest = FilesUtilities.GetRelativePath(dest, type, announcementId); new FileExtensionContentTypeProvider().TryGetContentType(dest, out var contentType); var newFile = Path.Combine(_webRootPath, dest); //try //{ // Path.GetDirectoryName(newFile).CreateDirectory(); // using (var stream = new FileStream(newFile, FileMode.Create)) // { // using (var readStream = file.OpenReadStream()) // { // lock (dest) // { // readStream.CopyToAsync(stream).Wait(); // } // } // } // var waterMarkDest = SetWaterMark(Path.GetFileName(dest), type, announcementId); // if (announcementId != 0) // await Callback(Path.GetFileName(waterMarkDest), announcementId, announcementPhotoType); // if (announcementPhotoType == AnnouncementPhotoType.OtherDocumentations) // ConvertPdf(Path.GetFileName(newFile), type, announcementId); //} string newFilePath = null; try { Path.GetDirectoryName(newFile).CreateDirectory(); lock (dest) { using (var stream = new FileStream(newFile, FileMode.Create)) { file.CopyToAsync(stream).Wait(); } if (type != UploadType.AnnouncementDocument && !contentType.Contains("video")) { newFilePath = Exif(newFile, file.FileName, type, announcementId); } } string waterMarkDest = null; if (announcementPhotoType == AttachmentType.OtherImages && announcementId != 0) { if (!contentType.Contains("video")) { waterMarkDest = SetWaterMark(Path.GetFileName(newFilePath), type, announcementId); await Callback(Path.GetFileName(waterMarkDest), announcementId, announcementPhotoType); } else { await Callback(Path.GetFileName(dest), announcementId, announcementPhotoType); await Task.Factory.StartNew(() => GetThumbnail(newFile, type, announcementId), TaskCreationOptions.LongRunning); } } if (!contentType.Contains("video")) { RemoveFile(newFile, UploadType.AnnouncementPhoto, announcementId); } if (announcementPhotoType == AttachmentType.OtherDocumentations && announcementId != 0 && type != UploadType.MessageFiles) { ConvertPdf(Path.GetFileName(newFile), type, announcementId); await Callback(Path.GetFileName(dest), announcementId, announcementPhotoType); } //if (announcementId != 0 ) // await Callback(Path.GetFileName(waterMarkDest), announcementId, announcementPhotoType); //Blur(Path.GetFileName(newFile), type, announcementId); //#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed // Task.Factory.StartNew(() => Blur(Path.GetFileName(newFile), type, announcementId), TaskCreationOptions.LongRunning); //#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed } catch (Exception) { await Decrement(announcementId); RemoveFile(dest, type, announcementId); } }); return(true); }