public async Task AddImageAsync(Stream stream, string originalName, string userName) { var imageProps = UploadUtilities.GetImageProperties(originalName, userName); var imageBlob = _imagesContainer.GetBlockBlobReference(imageProps.FileName); await imageBlob.UploadFromStreamAsync(stream); await UploadUtilities.RecordImageUploadedAsync(_dbContext, imageProps.UploadId, imageProps.FileName, imageBlob.Uri.ToString(), imageProps.UserHash); }
public async Task AddImageAsync(Stream stream, string originalName, string userName) { await InitializeResourcesAsync(); UploadUtilities.GetImageProperties(originalName, userName, out string uploadId, out string fileName, out string userId); var imageBlob = _uploadContainer.GetBlockBlobReference(fileName); await imageBlob.UploadFromStreamAsync(stream); await UploadUtilities.RecordImageUploadedAsync(_dbContext, uploadId, fileName, imageBlob.Uri.ToString(), userId); }
public async Task AddImageAsync(Stream stream, string originalName, string userName) { var imageProps = UploadUtilities.GetImageProperties(originalName, userName); var localPath = Path.Combine(ImageFolder, imageProps.FileName); var imageUri = $"{ImageFolderUri}/{imageProps.FileName}"; using (var fileStream = File.Create(localPath)) { stream.Seek(0, SeekOrigin.Begin); await stream.CopyToAsync(fileStream); } await UploadUtilities.RecordImageUploadedAsync(_dbContext, imageProps.UploadId, imageProps.FileName, imageUri, imageProps.UserHash); }
public async Task AddImageAsync(Stream stream, string originalName, string userName) { UploadUtilities.GetImageProperties(originalName, userName, out string uploadId, out string fileName, out string userId); string localPath = Path.Combine(ImageFolder, fileName); string imageUri = ImageFolderUri + "/" + fileName; using (var fileStream = File.Create(localPath)) { stream.Seek(0, SeekOrigin.Begin); await stream.CopyToAsync(fileStream); } await UploadUtilities.RecordImageUploadedAsync(_dbContext, uploadId, fileName, imageUri, userId); }
public async Task AddImageAsync(Stream stream, string originalName, string userName) { await InitializeResourcesAsync(); UploadUtilities.GetImageProperties(originalName, userName, out string uploadId, out string fileName, out string userId); var imageBlob = _uploadContainer.GetBlockBlobReference(fileName); await imageBlob.UploadFromStreamAsync(stream); //added code: get the URI from _publicContainer instead of _uploadContainer string imagePath = imageBlob.Uri.ToString().Replace("images", "images-watermarked"); //store the _publicContainer URI instead of upload container URI //await UploadUtilities.RecordImageUploadedAsync(_dbContext, uploadId, fileName, imageBlob.Uri.ToString(), userId); await UploadUtilities.RecordImageUploadedAsync(_dbContext, uploadId, fileName, imagePath, userId); }
public ActionResult UploadFile(UploadDbFileModel model) { ViewBag.AccessModes = new SelectList(new Dictionary <short, string> { { (short)AccessMode.Any, AccessMode.Any.ToLocalizedString() }, { (short)AccessMode.None, AccessMode.None.ToLocalizedString() }, { (short)AccessMode.OnlyAuthenticated, AccessMode.OnlyAuthenticated.ToLocalizedString() } }, "Key", "Value", model.AccessMode); if (!ModelState.IsValid) { ModelState.AddModelError("", ValidationResources.InvalidState); return(View(model)); } if (model.ParentId.HasValue) { File parent = _fileService.GetFileById(model.ParentId.Value); if (parent == null) { return(NotFoundView()); } if (!UserIsAllowedToCrud(parent)) { return(AccessDeniedView()); } ViewBag.Parent = parent; } if (string.IsNullOrWhiteSpace(model.FileName)) { model.FileName = model.PostedFile.FileName; } const string extensions = ".7z|.aiff|.asf|.avi|.bmp|.csv|.doc|.docx|.fla|.flv|.gif|.gz|.gzip|.jpeg|.jpg|.mid|.mov|.mp3|.mp4|.mpc|.mpeg|.mpg|.ods|.odt|.pdf|.png|.ppt|.pxd|.qt|.ram|.rar|.rm|.rmi|.rmvb|.rtf|.sdc|.sitd|.swf|.sxc|.sxw|.tar|.tgz|.tif|.tiff|.txt|.vsd|.wav|.wma|.wmv|.xls|.xml|.zip"; if (Path.GetExtension(model.FileName) == "") { model.FileName += Path.GetExtension(model.PostedFile.FileName); } if (model.ContentLength == 0 || extensions.Split('|').All(e => string.Compare(e, Path.GetExtension(model.FileName), StringComparison.OrdinalIgnoreCase) != 0)) { ModelState.AddModelError("", ValidationResources.SelectedFileIsInvalid); return(View(model)); } var file = new File { Uploader = _webHelper.GetCurrentUser(HttpContext), AccessModeShort = model.AccessMode, CreateDate = DateTime.UtcNow, Name = model.FileName, ContentType = model.ContentType, Size = model.ContentLength, IsPublished = true, ParentId = model.ParentId }; _fileService.SaveFile(file); bool isSaved; try { isSaved = _unitOfWork.SaveChanges() > 0; } catch { isSaved = false; } if (isSaved) { Logger.SaveLog(new CreateFileProvider(file)); string targetPath = Server.MapPath(Constants.UploadsUrl); UploadUtilities.Save(model.PostedFile, targetPath, file.Guid.ToString()); return(RedirectToAction("List", new { file.ParentId, page = 1 })); } ModelState.AddModelError("", ValidationResources.UploadFileFailure); return(View(model)); }
public ActionResult UploadFile(UploadFileModel model) { model.TargetUrl = "/" + model.TargetUrl; bool processParent = model.TargetUrl != Constants.CdnUrl; model.CurrentEntry = new EntryModel(model.TargetUrl, true, processParent); if (model.TargetUrl.Contains("..") || !model.TargetUrl.StartsWith(Constants.CdnUrl)) { return(NotFoundView()); } if (!ModelState.IsValid) { ModelState.AddModelError("", ValidationResources.InvalidState); return(View(model)); } if (string.IsNullOrWhiteSpace(model.FileName)) { model.FileName = model.PostedFile.FileName; } string targetPath = Server.MapPath(model.TargetUrl); const string extensions = ".7z|.aiff|.asf|.avi|.bmp|.csv|.doc|.docx|.fla|.flv|.gif|.gz|.gzip|.jpeg|.jpg|.mid|.mov|.mp3|.mp4|.mpc|.mpeg|.mpg|.ods|.odt|.pdf|.png|.ppt|.pxd|.qt|.ram|.rar|.rm|.rmi|.rmvb|.rtf|.sdc|.sitd|.swf|.sxc|.sxw|.tar|.tgz|.tif|.tiff|.txt|.vsd|.wav|.wma|.wmv|.xls|.xml|.zip"; if (Path.GetExtension(model.FileName) == "") { model.FileName += Path.GetExtension(model.PostedFile.FileName); } if (model.ContentLength == 0 || extensions.Split('|').All(e => string.Compare(e, Path.GetExtension(model.FileName), StringComparison.OrdinalIgnoreCase) != 0)) { ModelState.AddModelError("", ValidationResources.SelectedFileIsInvalid); return(View(model)); } bool isSaved; if (System.IO.File.Exists(Path.Combine(targetPath, model.FileName))) { ModelState.AddModelError("", ValidationResources.DuplicateFileName); return(View(model)); } try { UploadUtilities.Save(model.PostedFile, targetPath, model.FileName); isSaved = true; } catch { isSaved = false; } if (isSaved) { Logger.SaveLog(new UploadFileProvider(model)); return(RedirectToAction("List", new { currentUrl = model.TargetUrl })); } ModelState.AddModelError("", ValidationResources.UploadFileFailure); return(View(model)); }
//[ValidateAntiForgeryToken] public ActionResult Upload(HttpPostedFileBase upload, string CKEditorFuncNum, string CKEditor, string langCode) { string url; string message; string output; if (upload == null) { return(null); } const string extensions = ".7z|.aiff|.asf|.avi|.bmp|.csv|.doc|.docx|.fla|.flv|.gif|.gz|.gzip|.jpeg|.jpg|.mid|.mov|.mp3|.mp4|.mpc|.mpeg|.mpg|.ods|.odt|.pdf|.png|.ppt|.pxd|.qt|.ram|.rar|.rm|.rmi|.rmvb|.rtf|.sdc|.sitd|.swf|.sxc|.sxw|.tar|.tgz|.tif|.tiff|.txt|.vsd|.wav|.wma|.wmv|.xls|.xml|.zip"; if (upload.ContentLength == 0 || upload.ContentLength > 1000000 || extensions.Split('|').All(e => e != Path.GetExtension(upload.FileName)) || !UploadUtilities.IsValidImageBinary(upload.InputStream)) { message = ValidationResources.SelectedFileIsInvalid; output = BuildOutput(CKEditorFuncNum, null, message); return(Content(output)); } var file = new File { Uploader = _webHelper.GetCurrentUser(HttpContext), AccessMode = AccessMode.Any, CreateDate = DateTime.UtcNow, Name = upload.FileName, ContentType = upload.ContentType, Size = upload.ContentLength, IsPublished = true }; _fileService.SaveFile(file); bool isSaved; try { isSaved = _unitOfWork.SaveChanges() > 0; } catch { isSaved = false; } if (isSaved) { Logger.SaveLog(new CreateFileProvider(file)); string targetPath = Server.MapPath(Constants.UploadsUrl); UploadUtilities.Save(upload, targetPath, file.Guid.ToString()); url = Url.RouteUrl("Download", new { file.Guid, fn = file.Name }); message = ValidationResources.UploadFileSuccess; output = BuildOutput(CKEditorFuncNum, url, message); return(Content(output)); } message = ValidationResources.UploadFileFailure; output = BuildOutput(CKEditorFuncNum, null, message); return(Content(output)); }
public ActionResult Create(Carousel carousel) { if (!ModelState.IsValid) { ModelState.AddModelError("", ValidationResources.InvalidState); return(ViewOrPartialView(carousel)); } if (carousel.Slide == null) { ModelState.AddModelError("Slide", ValidationResources.SlideImageRequired); return(ViewOrPartialView(carousel)); } if (carousel.Slide.ContentLength > 1000000) { ModelState.AddModelError("Slide", ValidationResources.SlideImageLength); return(ViewOrPartialView(carousel)); } if (!UploadUtilities.IsValidImageBinary(carousel.Slide.InputStream)) { ModelState.AddModelError("Slide", ValidationResources.SlideImageInvalidBinary); return(ViewOrPartialView(carousel)); } _carouselService.SaveCarousel(carousel); bool isSaved; try { isSaved = _unitOfWork.SaveChanges() > 0; } catch { isSaved = false; } if (isSaved) { Logger.SaveLog(new CreateCarouselProvider(carousel)); } else { ModelState.AddModelError("", ValidationResources.CreationFailure); return(ViewOrPartialView(carousel)); } UploadUtilities.TryToSaveImage(carousel.Slide, Constants.CarouselsUrl, carousel.Id.ToString()); if (IsReferrerValid()) { return(Redirect(Request.UrlReferrer.AbsolutePath)); } return(RedirectToAction("List", new { page = 1 })); }
public ActionResult EditPost(int id) { Carousel dbCarousel = _carouselService.GetCarouselById(id); if (dbCarousel == null) { return(EntityNotFoundView()); } TryUpdateModel(dbCarousel, new[] { "Title", "Description", "LinkUrl", "Slide", "DisplayOrder" }); if (!TryValidateModel(dbCarousel)) { ModelState.AddModelError("", ValidationResources.InvalidState); return(ViewOrPartialView(dbCarousel)); } if (dbCarousel.Slide != null && dbCarousel.Slide.ContentLength > 1000000) { ModelState.AddModelError("Slide", ValidationResources.SlideImageLength); return(ViewOrPartialView(dbCarousel)); } bool isSaved = true; try { isSaved = _unitOfWork.SaveChanges() > 0; } catch { isSaved = false; } if (isSaved) { Logger.SaveLog(new UpdateCarouselProvider(dbCarousel)); } else { ModelState.AddModelError("", ValidationResources.UpdateFailure); return(ViewOrPartialView(dbCarousel)); } if (dbCarousel.Slide != null) { UploadUtilities.TryToSaveImage(dbCarousel.Slide, Constants.CarouselsUrl, dbCarousel.Id.ToString()); } if (IsReferrerValid()) { return(Redirect(Request.UrlReferrer.AbsolutePath)); } return(RedirectToAction("List", new { page = 1 })); }