public static void Upload(string tenant, int userId, HttpPostedFileBase file) { if (file == null) { throw new AttachmentException(Resources.NoFileWasUploaded); } string path = $"/Tenants/{tenant}/Areas/MixERP.Social/avatars"; path = PathMapper.MapPath(path); if (path == null) { Log.Warning("Could not upload resource because the avatar directory {directory} does not exist.", path); throw new AttachmentException(Resources.CouldNotFindAvatarDirectory); } if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string fileName = Path.GetFileName(file.FileName); if (fileName == null) { Log.Warning("Could not upload resource because the posted avatar file name is null or invalid."); throw new AttachmentException(Resources.InvalidFileName); } var allowed = FrapidConfig.GetAllowedUploadExtensions(tenant); string extension = Path.GetExtension(fileName); if (!allowed.Contains(extension)) { Log.Warning("Could not upload avatar resource because the uploaded file {file} has invalid extension.", fileName); throw new AttachmentException(Resources.AccessIsDenied); } var dir = new DirectoryInfo(path); foreach ( var f in dir.GetFiles().Where(f => Path.GetFileNameWithoutExtension(f.Name).Equals(userId.ToString()))) { f.Delete(); } var stream = file.InputStream; path = Path.Combine(path, userId + extension); using (var fileStream = File.Create(path)) { stream.CopyTo(fileStream); } }
public void Upload() { if (string.IsNullOrWhiteSpace(this.ThemeName)) { throw new ArgumentNullException(nameof(this.ThemeName)); } string tenant = DbConvention.GetTenant(); string path = $"~/Tenants/{tenant}/Areas/Frapid.WebsiteBuilder/Themes/{this.ThemeName}"; path = HostingEnvironment.MapPath(path); if (path == null || !Directory.Exists(path)) { Log.Warning("Could not upload resource because the directory {directory} does not exist.", path); throw new ResourceUploadException("Invalid path. Check the log for more details."); } path = Path.Combine(path, this.Container); if (!Directory.Exists(path)) { Log.Warning("Could not upload resource because the directory {directory} does not exist.", path); throw new ResourceUploadException("Invalid path. Check application logs for more information."); } string fileName = Path.GetFileName(this.PostedFile.FileName); if (fileName == null) { Log.Warning("Could not upload resource because the posted file name is null or invalid."); throw new ResourceUploadException( "Could not upload resource because the posted file name is null or invalid."); } var allowed = FrapidConfig.GetAllowedUploadExtensions(DbConvention.GetTenant()); string extension = Path.GetExtension(fileName); if (!allowed.Contains(extension)) { Log.Warning("Could not upload resource because the uploaded file {file} has invalid extension.", fileName); throw new ResourceUploadException( "Could not upload resource because the uploaded file has invalid extension."); } var stream = this.PostedFile.InputStream; path = Path.Combine(Path.Combine(path, fileName)); using (var fileStream = File.Create(path)) { stream.CopyTo(fileStream); } }
public void Upload(string tenant) { string path = $"~/Tenants/{tenant}"; path = HostingEnvironment.MapPath(path); if (path == null || !Directory.Exists(path)) { Log.Warning("Could not upload resource because the directory {directory} does not exist.", path); throw new ResourceUploadException(I18N.InvalidPathCheckLog); } path = Path.Combine(path, this.Container); if (!Directory.Exists(path)) { Log.Warning("Could not upload resource because the directory {directory} does not exist.", path); throw new ResourceUploadException(I18N.InvalidPathCheckLog); } string fileName = Path.GetFileName(this.PostedFile.FileName); if (fileName == null) { Log.Warning("Could not upload resource because the posted file name is null or invalid."); throw new ResourceUploadException(I18N.CouldNotUploadInvalidFileName); } var allowed = FrapidConfig.GetAllowedUploadExtensions(tenant); string extension = Path.GetExtension(fileName); if (!allowed.Contains(extension)) { Log.Warning("Could not upload resource because the uploaded file {file} has invalid extension.", fileName); throw new ResourceUploadException(I18N.CouldNotUploadInvalidFileExtension); } var stream = this.PostedFile.InputStream; path = Path.Combine(Path.Combine(path, fileName)); using (var fileStream = File.Create(path)) { stream.CopyTo(fileStream); } }
public ActionResult Post() { var area = new AreaRegistration(); var allowed = FrapidConfig.GetAllowedUploadExtensions(this.Tenant); var uploader = new Uploader(Log.Logger, area, this.Request.Files, this.Tenant, allowed); try { string fileName = uploader.Upload(); fileName = "/dashboard/config/services/attachments/" + fileName; return(this.Ok(fileName)); } catch (UploadException ex) { return(this.Failed(ex.Message, HttpStatusCode.BadRequest)); } }
public ActionResult Get(string resource = "") { if (string.IsNullOrWhiteSpace(resource)) { return(this.HttpNotFound()); } string tenant = DbConvention.GetTenant(); var allowed = FrapidConfig.GetMyAllowedResources(tenant); if (string.IsNullOrWhiteSpace(resource) || allowed.Count().Equals(0)) { return(this.HttpNotFound()); } string directory = HostingEnvironment.MapPath($"/Tenants/{tenant}"); if (directory == null) { return(this.HttpNotFound()); } string path = Path.Combine(directory, resource); if (!System.IO.File.Exists(path)) { return(this.HttpNotFound()); } string extension = Path.GetExtension(path); if (!allowed.Contains(extension)) { return(this.HttpNotFound()); } string mimeType = this.GetMimeType(path); return(this.File(path, mimeType)); }
public ActionResult GetImage(string relativePath = "", int width = 0, int height = 0) { if (string.IsNullOrWhiteSpace(relativePath)) { return(this.HttpNotFound()); } var allowed = FrapidConfig.GetAllowedImages(this.Tenant); if (string.IsNullOrWhiteSpace(relativePath) || allowed.Length.Equals(0)) { return(this.HttpNotFound()); } string directory = HostingEnvironment.MapPath(Configuration.GetCurrentThemePath(this.Tenant)); if (directory == null) { return(this.HttpNotFound()); } string path = Path.Combine(directory, relativePath); if (!System.IO.File.Exists(path)) { return(this.HttpNotFound()); } string extension = Path.GetExtension(path); if (!allowed.Contains(extension)) { return(this.HttpNotFound()); } string mimeType = this.GetMimeType(path); return(this.File(BitmapHelper.ResizeCropExcess(path, width, height), mimeType)); }
public static UploadedFile Upload(string tenant, string uploadDirectory, HttpPostedFileBase file) { if (file == null) { throw new UploadException(Resources.NoFileWasUploaded); } string fileName = Path.GetFileName(file.FileName); string extension = Path.GetExtension(fileName); string savedFile = Guid.NewGuid() + extension; if (string.IsNullOrEmpty(fileName)) { Log.Information("Could not upload resource because the posted attachment file name is null or invalid."); throw new UploadException(Resources.InvalidFile); } var allowed = FrapidConfig.GetAllowedUploadExtensions(tenant); if (!allowed.Contains(extension)) { Log.Warning("Could not upload attachment resource because the uploaded file {file} has invalid extension.", fileName); throw new UploadException(Resources.InvalidFileExtension); } var stream = file.InputStream; string path = Path.Combine(uploadDirectory, savedFile); using (var fileStream = File.Create(path)) { stream.CopyTo(fileStream); } return(new UploadedFile { OriginalFileName = fileName, FileName = savedFile }); }