/// <summary> /// Add a file to be attached to the this.message. /// </summary> /// <param name="attachment">A file attachment object with the file contents and filename.</param> /// <exception cref="ArgumentNullException">The attachment cannot be null or empty.</exception> /// <exception cref="ArgumentOutOfRangeException">The maximum message size cannot exceed 25 MB in size.</exception> /// <returns>The instance of the builder.</returns> public IMessageBuilder AddAttachment(IFileAttachment attachment) { if (attachment == null) { throw new ArgumentNullException(nameof(attachment), "Attachment cannot be null!"); } if (this.message.FileAttachments == null) { this.message.FileAttachments = new Collection <IFileAttachment>(); } var size = this.messageSize + attachment.Data.Length; if (size > MAX_TOTAL_MESSAGE_SIZE) { throw new ArgumentOutOfRangeException(nameof(this.messageSize), size, "Cannot exceed total message size of 25MB!"); } this.messageSize += attachment.Data.Length; this.message.FileAttachments.Add(attachment); return(this); }
private Task <bool> FileTypeIsValid(IFileAttachment file, CancellationToken cancellationToken) { var supportedTypes = GetSupportedFileTypes(); var type = Path.GetExtension(file.FileName).Replace(".", ""); return(Task.Run(() => supportedTypes.Contains(type, StringComparer.OrdinalIgnoreCase), cancellationToken)); }
/// <summary> /// Add the in-line image file attachment to be written into the this.message. /// </summary> /// <param name="image">The file attachment object with the file contents and filename.</param> /// <exception cref="ArgumentNullException">Image cannot be null or empty.</exception> /// <exception cref="ArgumentOutOfRangeException">The maximum message size cannot of 25 MB cannot be exceeded.</exception> /// <returns>The instance of the builder.</returns> public IMessageBuilder AddInlineImage(IFileAttachment image) { if (image == null) { throw new ArgumentNullException(nameof(image), "Image cannot be null!"); } if (this.message.FileInline == null) { this.message.FileInline = new Collection <IFileAttachment>(); } var size = this.messageSize + image.Data.Length; if (size > MAX_TOTAL_MESSAGE_SIZE) { throw new ArgumentOutOfRangeException(nameof(this.messageSize), size, "Cannot exceed total message size of 25MB!"); } this.messageSize += image.Data.Length; this.message.FileInline.Add(image); return(this); }
///<inheritdoc/> public void AddAttachment(IFileAttachment file) { if (_files.FirstOrDefault(x => x.Filename.ToLower() == file.Filename.ToLower()) == null) { _files.Add(file); } }
// Generates the cell containing the hidden inputs for binding private static string EditRowHiddenInputs(IFileAttachment attachment, string propertyName, int index) { StringBuilder html = new StringBuilder(); // Generate the input for the collection indexer TagBuilder indexer = new TagBuilder("input"); indexer.MergeAttribute("type", "hidden"); indexer.MergeAttribute("value", index.ToString()); indexer.MergeAttribute("name", string.Format("{0}.Index", propertyName)); html.Append(indexer.ToString()); // Generate the input for the file ID TagBuilder id = new TagBuilder("input"); id.MergeAttribute("value", attachment == null ? string.Empty : attachment.ID.ToString()); id.MergeAttribute("type", "hidden"); id.MergeAttribute("name", string.Format("{0}[{1}].ID", propertyName, index)); html.Append(id.ToString()); // Generate the input for the file name and path TagBuilder fileName = new TagBuilder("input"); fileName.MergeAttribute("value", attachment == null ? string.Empty : attachment.VirtualPath); fileName.MergeAttribute("type", "hidden"); fileName.MergeAttribute("name", string.Format("{0}[{1}].FilePath", propertyName, index)); html.Append(fileName.ToString()); // Generate the input for the files display name TagBuilder displayName = new TagBuilder("input"); displayName.MergeAttribute("value", attachment == null ? string.Empty : attachment.DisplayName); displayName.MergeAttribute("type", "hidden"); displayName.MergeAttribute("name", string.Format("{0}[{1}].DisplayName", propertyName, index)); html.Append(displayName.ToString()); // Generate the input for the file size TagBuilder size = new TagBuilder("input"); size.MergeAttribute("value", attachment == null ? string.Empty : attachment.Size.ToString()); size.MergeAttribute("type", "hidden"); size.MergeAttribute("name", string.Format("{0}[{1}].Size", propertyName, index)); html.Append(size.ToString()); // Generate the input for the file status TagBuilder status = new TagBuilder("input"); status.AddCssClass("file-status"); status.MergeAttribute("value", attachment == null ? string.Empty : ((int)attachment.Status).ToString()); status.MergeAttribute("type", "hidden"); status.MergeAttribute("name", string.Format("{0}[{1}].Status", propertyName, index)); html.Append(status.ToString()); // Generate the table cell TagBuilder cell = new TagBuilder("td"); cell.InnerHtml = html.ToString(); // Return the html return(cell.ToString()); }
// Generates a row in the tbody element private static string DisplayRow(HtmlHelper helper, Type modelType, IFileAttachment attachment, IEnumerable <string> extraColumns, string actionName, string controllerName) { // Get the ModelMetadata for the attachment ModelMetadata itemMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => attachment, modelType); StringBuilder html = new StringBuilder(); // Generate link object routeValues = new { ID = attachment.ID.Value }; object htmlAttributes = new { target = "_blank" }; var link = helper.ActionLink(attachment.DisplayName, actionName, controllerName, routeValues, htmlAttributes); // Add table cells TagBuilder cell = new TagBuilder("td"); cell.InnerHtml = link.ToString(); html.Append(cell.ToString()); foreach (string propertyName in extraColumns) { ModelMetadata metaData = itemMetadata.Properties.FirstOrDefault(x => x.PropertyName == propertyName); if (metaData.Model == null) { cell.InnerHtml = metaData.NullDisplayText; } else if (metaData.ModelType == typeof(bool) || metaData.ModelType == typeof(Nullable <bool>)) { cell.InnerHtml = (bool)metaData.Model ? "Yes" : "No"; } //else if (metaData.AdditionalValues.ContainsKey(_SelectListKey)) //{ // // This would only work if we get the IEnumerable<SelectListItem> and find the corresponding text // Would be better to use a separate view model for display vd edit //} else { string formatString = metaData.DisplayFormatString ?? "{0}"; cell.InnerHtml = String.Format(formatString, metaData.Model); } html.Append(cell.ToString()); } string size = String.Format("{0} kB", attachment.Size); if (attachment.Size >= 1024) { size = String.Format("{0:0.00} MB", attachment.Size / 1024F); } cell.InnerHtml = size; html.Append(cell.ToString()); // Generate table row TagBuilder row = new TagBuilder("tr"); row.InnerHtml = html.ToString(); return(row.ToString()); }
public IMessageBuilder AddAttachment(IFileAttachment file) { ThrowIf.IsArgumentNull(() => file); if (_message.FileAttachments == null) { _message.FileAttachments = new Collection <IFileAttachment>(); } //Add _message.FileAttachments.Add(file); return(this); }
/// <summary> /// Returns a FileResult to send the contents of a file to the response. /// </summary> /// <param name="attachment"> /// An IFileAttachment containing the properties of the file to download. /// </param> public static FileResult Download(IFileAttachment attachment) { // Validate if (attachment == null) { throw new ArgumentNullException(nullAttachment); } if (attachment.VirtualPath == null) { throw new ArgumentNullException(nullPath); } // Return FileResult return(Download(attachment.VirtualPath)); }
public async Task <string> SaveAsync(IFileAttachment file, FileDestinations type) { var directoryPath = _folders[type]; if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } var fileName = $"{Guid.NewGuid()}{Path.GetExtension(file.FileName)}"; var fullName = Path.Combine(directoryPath, fileName); await using (var stream = File.Create(fullName)) { await file.CopyToAsync(stream); } _sessionPaths.Add(fullName); return(fileName); }
// Generates the row for editing data in the visible tbody element private static string EditRow(HtmlHelper helper, Type modelType, IFileAttachment attachment, string propertyName, int index, IEnumerable <string> extraColumns, Dictionary <string, object> optionLists) { // Get the ModelMetadata for the attachment ModelMetadata itemMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => attachment, modelType); // Generate table cells StringBuilder html = new StringBuilder(); string displayName = TableCell(attachment.DisplayName); html.Append(displayName); string prefix = String.Format("{0}[{1}]", propertyName, index); string formControls = EditRowControlCells(helper, itemMetadata, prefix, extraColumns, optionLists); html.Append(formControls); string size = String.Format("{0} kB", attachment.Size); if (attachment.Size >= 1024) { size = String.Format("{0:0.00} MB", attachment.Size / 1024F); } string fileSize = TableCell(size); string button = ButtonCell(ButtonType.Delete); string inputs = EditRowHiddenInputs(attachment, propertyName, index); html.Append(fileSize); html.Append(button); html.Append(inputs); // Generate table row TagBuilder row = new TagBuilder("tr"); row.AddCssClass("edit-row"); if (attachment.Status == FileAttachmentStatus.Deleted) { row.AddCssClass("archived"); } row.InnerHtml = html.ToString(); return(row.ToString()); }
partial void ProjectUri_SetCondition(ref IFileAttachment instance, ref String setValue);
partial void LocalFile_SetCondition(ref IFileAttachment instance, ref Stream setValue);
partial void FileNameGUID_SetCondition(ref IFileAttachment instance, ref Guid setValue);
partial void AreaNodeUri_SetCondition(ref IFileAttachment instance, ref String setValue);
/// <summary> /// Uploads the file. /// </summary> /// <param name="fileAttachment">The file attachment.</param> /// <exception cref="DynCon.OSI.Core.Helpers.ToBeImplementedException"></exception> void IWorkItemServer.UploadFile(IFileAttachment fileAttachment) { throw new ToBeImplementedException(); }
public static FileAttachment GetInstance(IFileAttachment src) { return(default(FileAttachment)); }
private Task <bool> FileSizeIsValid(IFileAttachment file, CancellationToken cancellationToken) { return(Task.Run(() => file.FileSize <= CarPhotoConstraints.FileMaxSize, cancellationToken)); }