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 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 }); }