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