public void StoreAttachmentWithoutQuota(MailAttachment attachment) { try { if ((attachment.dataStream == null || attachment.dataStream.Length == 0) && (attachment.data == null || attachment.data.Length == 0)) { return; } if (string.IsNullOrEmpty(attachment.fileName)) { attachment.fileName = "attachment.ext"; } var storage = MailDataStore.GetDataStore(Tenant); storage.QuotaController = null; var ext = Path.GetExtension(attachment.fileName); attachment.storedName = MailUtil.CreateStreamId(); if (!string.IsNullOrEmpty(ext)) { attachment.storedName = Path.ChangeExtension(attachment.storedName, ext); } attachment.fileNumber = !string.IsNullOrEmpty(attachment.contentId) //Upload hack: embedded attachment have to be saved in 0 folder ? 0 : attachment.fileNumber; var attachmentPath = MailStoragePathCombiner.GerStoredFilePath(attachment); if (attachment.data != null) { using (var reader = new MemoryStream(attachment.data)) { var uploadUrl = storage.Save(attachmentPath, reader, attachment.fileName); attachment.storedFileUrl = MailStoragePathCombiner.GetStoredUrl(uploadUrl); } } else { var uploadUrl = storage.Save(attachmentPath, attachment.dataStream, attachment.fileName); attachment.storedFileUrl = MailStoragePathCombiner.GetStoredUrl(uploadUrl); } } catch (Exception e) { _logger.Error("StoreAttachmentWithoutQuota(). filename: {0}, ctype: {1} Exception:\r\n{2}\r\n", attachment.fileName, attachment.contentType, e.ToString()); throw; } }
public static AttachmentStream GetAttachmentStream(MailAttachment attachment) { if (attachment != null) { var storage = MailDataStore.GetDataStore(attachment.tenant); var attachmentPath = MailStoragePathCombiner.GerStoredFilePath(attachment); var result = new AttachmentStream { FileStream = storage.GetReadStream("", attachmentPath), FileName = attachment.fileName }; return(result); } throw new InvalidOperationException("Attachment not found"); }
public static string GetPreSignedUri(int fileId, int tenant, string user, string stream, int fileNumber, string fileName, IDataStore dataStore) { var attachmentPath = GetFileKey(user, stream, fileNumber, fileName); if (dataStore == null) { dataStore = MailDataStore.GetDataStore(tenant); } string url; if (dataStore is S3Storage) { var contentDispositionFileName = ContentDispositionUtil.GetHeaderValue(fileName, withoutBase: true); var headersForUrl = new [] { "Content-Disposition:" + contentDispositionFileName }; url = dataStore.GetPreSignedUri("", attachmentPath, TimeSpan.FromMinutes(10), headersForUrl).ToString(); } else { //TODO: Move url to config; attachmentPath = "/addons/mail/httphandlers/download.ashx"; var uriBuilder = new UriBuilder(CommonLinkUtility.GetFullAbsolutePath(attachmentPath)); if (uriBuilder.Uri.IsLoopback) { uriBuilder.Host = Dns.GetHostName(); } var query = uriBuilder.Query; query += "attachid=" + fileId + "&"; query += "stream=" + stream + "&"; query += FilesLinkUtility.AuthKey + "=" + EmailValidationKeyProvider.GetEmailKey(fileId + stream); url = uriBuilder.Uri + "?" + query; } return(url); }
public void StoreAttachmentWithoutQuota(int tenant, string user, MailAttachment attachment) { StoreAttachmentWithoutQuota(tenant, user, attachment, MailDataStore.GetDataStore(tenant)); }