public Stream GetFileStream(File file, long offset)
        {
            var fileToDownload = GetFileById(file.ID);

            if (fileToDownload == null)
            {
                throw new ArgumentNullException("file", FilesCommonResource.ErrorMassage_FileNotFound);
            }
            if (fileToDownload is ErrorEntry)
            {
                throw new Exception(((ErrorEntry)fileToDownload).Error);
            }

            var fileStream = fileToDownload.GetDataTransferAccessor().GetDownloadStream();

            if (fileStream != null && offset > 0)
            {
                if (!fileStream.CanSeek)
                {
                    var tempBuffer = TempStream.Create();

                    fileStream.CopyTo(tempBuffer);
                    tempBuffer.Flush();
                    tempBuffer.Seek(offset, SeekOrigin.Begin);

                    fileStream.Dispose();
                    return(tempBuffer);
                }

                fileStream.Seek(offset, SeekOrigin.Begin);
            }

            return(fileStream);
        }
Beispiel #2
0
        public Stream DownloadStream(BoxFile file, int offset = 0)
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            if (offset > 0 && file.Size.HasValue)
            {
                return(_boxClient.FilesManager.DownloadAsync(file.Id, startOffsetInBytes: offset, endOffsetInBytes: (int)file.Size - 1).Result);
            }

            var str = _boxClient.FilesManager.DownloadAsync(file.Id).Result;

            if (offset == 0)
            {
                return(str);
            }

            var tempBuffer = TempStream.Create();

            if (str != null)
            {
                str.CopyTo(tempBuffer);
                tempBuffer.Flush();
                tempBuffer.Seek(offset, SeekOrigin.Begin);

                str.Dispose();
            }

            return(tempBuffer);
        }
        public override void RunJob(DistributedTask _, CancellationToken cancellationToken)
        {
            base.RunJob(_, cancellationToken);

            using var scope = ThirdPartyOperation.CreateScope();
            var globalStore      = scope.ServiceProvider.GetService <GlobalStore>();
            var filesLinkUtility = scope.ServiceProvider.GetService <FilesLinkUtility>();

            using var stream = TempStream.Create();
            using (var zip = new ZipOutputStream(stream, true)
            {
                CompressionLevel = Ionic.Zlib.CompressionLevel.Level3,
                AlternateEncodingUsage = ZipOption.AsNecessary,
                AlternateEncoding = Encoding.UTF8,
            })
            {
                (ThirdPartyOperation as FileDownloadOperation <string>).CompressToZip(zip, stream, scope);
                (DaoOperation as FileDownloadOperation <int>).CompressToZip(zip, stream, scope);
            }

            if (stream != null)
            {
                stream.Position = 0;
                const string fileName = FileConstant.DownloadTitle + ".zip";
                var          store    = globalStore.GetStore();
                store.Save(
                    FileConstant.StorageDomainTmp,
                    string.Format(@"{0}\{1}", ((IAccount)Thread.CurrentPrincipal.Identity).ID, fileName),
                    stream,
                    "application/zip",
                    "attachment; filename=\"" + fileName + "\"");
                Status = string.Format("{0}?{1}=bulk", filesLinkUtility.FileHandlerPath, FilesLinkUtility.Action);
            }
        }
Beispiel #4
0
        public Stream GetFileStream(object id, int offset = 0)
        {
            var file = GetFileById(id);

            if (file is SharePointFileErrorEntry)
            {
                return(null);
            }
            var fileInfo = File.OpenBinaryDirect(clientContext, (string)id);

            clientContext.ExecuteQuery();

            var tempBuffer = TempStream.Create();

            using (var str = fileInfo.Stream)
            {
                if (str != null)
                {
                    str.CopyTo(tempBuffer);
                    tempBuffer.Flush();
                    tempBuffer.Seek(offset, SeekOrigin.Begin);
                }
            }

            return(tempBuffer);
        }
    private const int BufferSize = 2048;//NOTE: set to 2048 to fit in minimum tcp window

    public static Stream IronReadStream(this IDataStore store, string domain, string path, int tryCount)
    {
        var ms = TempStream.Create();

        IronReadToStream(store, domain, path, tryCount, ms);
        ms.Seek(0, SeekOrigin.Begin);
        return(ms);
    }
Beispiel #6
0
 public static byte[] IronRead(this IDataStore store, string domain, string path, int tryCount)
 {
     using (var ms = TempStream.Create())
     {
         IronReadToStream(store, domain, path, tryCount, ms);
         return(ms.GetStreamBuffer());
     }
 }
        private List <XElement> BackupDatabase(int tenant, ConnectionStringSettings connectionString, IDataWriteOperator writer)
        {
            var xml     = new List <XElement>();
            var errors  = 0;
            var timeout = TimeSpan.FromSeconds(1);

            using (var dbHelper = new DbHelper(connectionString))
            {
                var tables = dbHelper.GetTables();
                for (int i = 0; i < tables.Count; i++)
                {
                    var table = tables[i];
                    OnProgressChanged(table, (int)(i / (double)tables.Count * 100));

                    if (processedTables.Contains(table, StringComparer.InvariantCultureIgnoreCase))
                    {
                        continue;
                    }

                    xml.Add(new XElement(table));
                    DataTable dataTable = null;
                    while (true)
                    {
                        try
                        {
                            dataTable = dbHelper.GetTable(table, tenant);
                            break;
                        }
                        catch
                        {
                            errors++;
                            if (20 < errors)
                            {
                                throw;
                            }
                            Thread.Sleep(timeout);
                        }
                    }
                    foreach (DataColumn c in dataTable.Columns)
                    {
                        if (c.DataType == typeof(DateTime))
                        {
                            c.DateTimeMode = DataSetDateTime.Unspecified;
                        }
                    }

                    using (var file = TempStream.Create())
                    {
                        dataTable.WriteXml(file, XmlWriteMode.WriteSchema);
                        writer.WriteEntry(string.Format("{0}\\{1}\\{2}", Name, connectionString.Name, table).ToLower(), file);
                    }

                    processedTables.Add(table);
                }
            }
            return(xml);
        }
Beispiel #8
0
    private const int BufferSize = 2048; //NOTE: set to 2048 to fit in minimum tcp window

    public static Stream GetBuffered(this Stream srcStream)
    {
        if (srcStream == null)
        {
            throw new ArgumentNullException("srcStream");
        }
        if (!srcStream.CanSeek || srcStream.CanTimeout)
        {
            //Buffer it
            var memStream = TempStream.Create();
            srcStream.StreamCopyTo(memStream);
            memStream.Position = 0;
            return(memStream);
        }
        return(srcStream);
    }
        public Stream DownloadStream(DriveFile file, int offset = 0)
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            var downloadArg = string.Format("{0}?alt=media", file.Id);

            var ext = MimeMapping.GetExtention(file.MimeType);

            if (GoogleLoginProvider.GoogleDriveExt.Contains(ext))
            {
                var internalExt      = FileUtility.GetGoogleDownloadableExtension(ext);
                var requiredMimeType = MimeMapping.GetMimeMapping(internalExt);

                downloadArg = string.Format("{0}/export?mimeType={1}",
                                            file.Id,
                                            HttpUtility.UrlEncode(requiredMimeType));
            }

            var request = WebRequest.Create(GoogleLoginProvider.GoogleUrlFile + downloadArg);

            request.Method = "GET";
            request.Headers.Add("Authorization", "Bearer " + AccessToken);

            var response = (HttpWebResponse)request.GetResponse();

            if (offset == 0 && file.Size.HasValue && file.Size > 0)
            {
                return(new ResponseStream(response.GetResponseStream(), file.Size.Value));
            }

            var tempBuffer = TempStream.Create();

            using (var str = response.GetResponseStream())
            {
                if (str != null)
                {
                    str.CopyTo(tempBuffer);
                    tempBuffer.Flush();
                    tempBuffer.Seek(offset, SeekOrigin.Begin);
                }
            }

            return(tempBuffer);
        }
        public override Stream GetReadStream(string domain, string path, int offset)
        {
            var outputStream = TempStream.Create();

            var client = GetClient();

            client.GetObject(_private_container, MakePath(domain, path), outputStream);

            outputStream.Position = 0;

            if (0 < offset)
            {
                outputStream.Seek(Convert.ToInt64(offset), SeekOrigin.Begin);
            }

            return(outputStream);
        }
Beispiel #11
0
        public override System.IO.Stream GetReadStream(string domain, string path, int offset)
        {
            var tempStream = TempStream.Create();

            var storage = GetStorage();

            storage.DownloadObject(_bucket, MakePath(domain, path), tempStream, null, null);

            if (offset > 0)
            {
                tempStream.Seek(offset, SeekOrigin.Begin);
            }

            tempStream.Position = 0;

            return(tempStream);
        }
        public override async Task <Stream> GetReadStreamAsync(string domain, string path, int offset)
        {
            var tempStream = TempStream.Create();

            var storage = GetStorage();

            await storage.DownloadObjectAsync(_bucket, MakePath(domain, path), tempStream);

            if (offset > 0)
            {
                tempStream.Seek(offset, SeekOrigin.Begin);
            }

            tempStream.Position = 0;

            return(tempStream);
        }
Beispiel #13
0
        private void DoBackupStorage(IDataWriteOperator writer, List <IGrouping <string, BackupFileInfo> > fileGroups)
        {
            Logger.Debug("begin backup storage");

            foreach (var group in fileGroups)
            {
                var filesProcessed = 0;
                var filesCount     = group.Count();

                foreach (var file in group)
                {
                    var storage = StorageFactory.GetStorage(ConfigPath, TenantId.ToString(), group.Key);
                    var file1   = file;
                    ActionInvoker.Try(state =>
                    {
                        var f = (BackupFileInfo)state;
                        using (var fileStream = storage.GetReadStream(f.Domain, f.Path))
                        {
                            writer.WriteEntry(file1.GetZipKey(), fileStream);
                        }
                    }, file, 5, error => Logger.WarnFormat("can't backup file ({0}:{1}): {2}", file1.Module, file1.Path, error));

                    SetCurrentStepProgress((int)(++filesProcessed * 100 / (double)filesCount));
                }
            }

            var restoreInfoXml = new XElement(
                "storage_restore",
                fileGroups
                .SelectMany(group => group.Select(file => (object)file.ToXElement()))
                .ToArray());

            using (var tmpFile = TempStream.Create())
            {
                restoreInfoXml.WriteTo(tmpFile);
                writer.WriteEntry(KeyHelper.GetStorageRestoreInfoZipKey(), tmpFile);
            }

            Logger.Debug("end backup storage");
        }
        public Stream DownloadStream(string filePath, int offset = 0)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException("file");
            }

            using (var response = _dropboxClient.Files.DownloadAsync(filePath).Result)
            {
                var tempBuffer = TempStream.Create();
                using (var str = response.GetContentAsStreamAsync().Result)
                {
                    if (str != null)
                    {
                        str.CopyTo(tempBuffer);
                        tempBuffer.Flush();
                        tempBuffer.Seek(offset, SeekOrigin.Begin);
                    }
                }
                return(tempBuffer);
            }
        }
        public override void RunJob(DistributedTask distributedTask, CancellationToken cancellationToken)
        {
            base.RunJob(distributedTask, cancellationToken);

            using var scope = ThirdPartyOperation.CreateScope();
            var scopeClass = scope.ServiceProvider.GetService <FileDownloadOperationScope>();

            var(globalStore, filesLinkUtility, _, _, _) = scopeClass;
            using var stream = TempStream.Create();

            var writerOptions = new ZipWriterOptions(CompressionType.Deflate);

            writerOptions.ArchiveEncoding.Default = Encoding.UTF8;
            writerOptions.DeflateCompressionLevel = SharpCompress.Compressors.Deflate.CompressionLevel.Level3;

            using (var zip = WriterFactory.Open(stream, ArchiveType.Zip, writerOptions))
            {
                (ThirdPartyOperation as FileDownloadOperation <string>).CompressToZip(zip, stream, scope);
                (DaoOperation as FileDownloadOperation <int>).CompressToZip(zip, stream, scope);
            }

            if (stream != null)
            {
                stream.Position = 0;
                const string fileName = FileConstant.DownloadTitle + ".zip";
                var          store    = globalStore.GetStore();
                store.Save(
                    FileConstant.StorageDomainTmp,
                    string.Format(@"{0}\{1}", ((IAccount)Thread.CurrentPrincipal.Identity).ID, fileName),
                    stream,
                    "application/zip",
                    "attachment; filename=\"" + fileName + "\"");
                Status = string.Format("{0}?{1}=bulk", filesLinkUtility.FileHandlerPath, FilesLinkUtility.Action);
            }

            FillDistributedTask();
            TaskInfo.PublishChanges();
        }
Beispiel #16
0
        private void ExportAllData()
        {
            using (var stream = TempStream.Create())
            {
                var contactDao     = _daoFactory.GetContactDao();
                var contactInfoDao = _daoFactory.GetContactInfoDao();
                var dealDao        = _daoFactory.GetDealDao();
                var casesDao       = _daoFactory.GetCasesDao();
                var taskDao        = _daoFactory.GetTaskDao();
                var historyDao     = _daoFactory.GetRelationshipEventDao();
                var invoiceItemDao = _daoFactory.GetInvoiceItemDao();

                _totalCount += contactDao.GetAllContactsCount();
                _totalCount += dealDao.GetDealsCount();
                _totalCount += casesDao.GetCasesCount();
                _totalCount += taskDao.GetAllTasksCount();
                _totalCount += historyDao.GetAllItemsCount();
                _totalCount += invoiceItemDao.GetInvoiceItemsCount();

                using (var zipStream = new ZipOutputStream(stream, true))
                {
                    zipStream.PutNextEntry("contacts.csv");
                    var contactData  = contactDao.GetAllContacts();
                    var contactInfos = new StringDictionary();
                    contactInfoDao.GetAll()
                    .ForEach(item =>
                    {
                        var contactInfoKey = String.Format("{0}_{1}_{2}", item.ContactID, (int)item.InfoType, item.Category);
                        if (contactInfos.ContainsKey(contactInfoKey))
                        {
                            contactInfos[contactInfoKey] += "," + item.Data;
                        }
                        else
                        {
                            contactInfos.Add(contactInfoKey, item.Data);
                        }
                    });

                    var zipEntryData = new MemoryStream(Encoding.UTF8.GetBytes(ExportContactsToCSV(contactData, contactInfos)));
                    zipEntryData.StreamCopyTo(zipStream);

                    zipStream.PutNextEntry("oppotunities.csv");
                    var dealData = dealDao.GetAllDeals();
                    zipEntryData = new MemoryStream(Encoding.UTF8.GetBytes(ExportDealsToCSV(dealData)));
                    zipEntryData.StreamCopyTo(zipStream);

                    zipStream.PutNextEntry("cases.csv");
                    var casesData = casesDao.GetAllCases();
                    zipEntryData = new MemoryStream(Encoding.UTF8.GetBytes(ExportCasesToCSV(casesData)));
                    zipEntryData.StreamCopyTo(zipStream);

                    zipStream.PutNextEntry("tasks.csv");
                    var taskData = taskDao.GetAllTasks();
                    zipEntryData = new MemoryStream(Encoding.UTF8.GetBytes(ExportTasksToCSV(taskData)));
                    zipEntryData.StreamCopyTo(zipStream);

                    zipStream.PutNextEntry("history.csv");
                    var historyData = historyDao.GetAllItems();
                    zipEntryData = new MemoryStream(Encoding.UTF8.GetBytes(ExportHistoryToCSV(historyData)));
                    zipEntryData.StreamCopyTo(zipStream);

                    zipStream.PutNextEntry("products_services.csv");
                    var invoiceItemData = invoiceItemDao.GetAll();
                    zipEntryData = new MemoryStream(Encoding.UTF8.GetBytes(ExportInvoiceItemsToCSV(invoiceItemData)));
                    zipEntryData.StreamCopyTo(zipStream);

                    zipStream.Flush();
                    zipStream.Close();

                    stream.Position = 0;
                }

                var assignedURI = _dataStore.SavePrivate(String.Empty, "exportdata.zip", stream, DateTime.Now.AddDays(1));
                Status = assignedURI;
                _notifyClient.SendAboutExportCompleted(_author.ID, assignedURI);
                Complete();
            }
        }
Beispiel #17
0
        private void DoBackupModule(IDataWriteOperator writer, DbFactory dbFactory, IModuleSpecifics module)
        {
            Logger.DebugFormat("begin saving data for module {0}", module.ModuleName);
            var tablesToProcess = module.Tables.Where(t => !IgnoredTables.Contains(t.Name) && t.InsertMethod != InsertMethod.None).ToList();
            var tablesCount     = tablesToProcess.Count;
            var tablesProcessed = 0;

            using (var connection = dbFactory.OpenConnection())
            {
                foreach (var table in tablesToProcess)
                {
                    Logger.DebugFormat("begin load table {0}", table.Name);
                    using (var data = new DataTable(table.Name))
                    {
                        ActionInvoker.Try(
                            state =>
                        {
                            data.Clear();
                            int counts;
                            var offset = 0;
                            do
                            {
                                var t                     = (TableInfo)state;
                                var dataAdapter           = dbFactory.CreateDataAdapter();
                                dataAdapter.SelectCommand = module.CreateSelectCommand(connection.Fix(), TenantId, t, Limit, offset).WithTimeout(600);
                                counts                    = ((DbDataAdapter)dataAdapter).Fill(data);
                                offset                   += Limit;
                            } while (counts == Limit);
                        },
                            table,
                            maxAttempts: 5,
                            onFailure: error => { throw ThrowHelper.CantBackupTable(table.Name, error); },
                            onAttemptFailure: error => Logger.Warn("backup attempt failure: {0}", error));

                        foreach (var col in data.Columns.Cast <DataColumn>().Where(col => col.DataType == typeof(DateTime)))
                        {
                            col.DateTimeMode = DataSetDateTime.Unspecified;
                        }

                        module.PrepareData(data);

                        Logger.DebugFormat("end load table {0}", table.Name);

                        Logger.DebugFormat("begin saving table {0}", table.Name);

                        using (var file = TempStream.Create())
                        {
                            data.WriteXml(file, XmlWriteMode.WriteSchema);
                            data.Clear();

                            writer.WriteEntry(KeyHelper.GetTableZipKey(module, data.TableName), file);
                        }

                        Logger.DebugFormat("end saving table {0}", table.Name);
                    }

                    SetCurrentStepProgress((int)((++tablesProcessed * 100) / (double)tablesCount));
                }
            }
            Logger.DebugFormat("end saving data for module {0}", module.ModuleName);
        }
Beispiel #18
0
        private Stream CompressToZip(IServiceScope scope, ItemNameValueCollection entriesPathId)
        {
            var setupInfo           = scope.ServiceProvider.GetService <SetupInfo>();
            var fileConverter       = scope.ServiceProvider.GetService <FileConverter>();
            var filesMessageService = scope.ServiceProvider.GetService <FilesMessageService>();

            var stream = TempStream.Create();

            using (var zip = new ZipOutputStream(stream, true))
            {
                zip.CompressionLevel       = Ionic.Zlib.CompressionLevel.Level3;
                zip.AlternateEncodingUsage = Ionic.Zip.ZipOption.AsNecessary;
                zip.AlternateEncoding      = Encoding.UTF8;

                foreach (var path in entriesPathId.AllKeys)
                {
                    var counter = 0;
                    foreach (var entryId in entriesPathId[path])
                    {
                        if (CancellationToken.IsCancellationRequested)
                        {
                            zip.Dispose();
                            stream.Dispose();
                            CancellationToken.ThrowIfCancellationRequested();
                        }

                        var newtitle = path;

                        File file         = null;
                        var  convertToExt = string.Empty;

                        if (!string.IsNullOrEmpty(entryId))
                        {
                            FileDao.InvalidateCache(entryId);
                            file = FileDao.GetFile(entryId);

                            if (file == null)
                            {
                                Error = FilesCommonResource.ErrorMassage_FileNotFound;
                                continue;
                            }

                            if (file.ContentLength > setupInfo.AvailableFileSize)
                            {
                                Error = string.Format(FilesCommonResource.ErrorMassage_FileSizeZip, FileSizeComment.FilesSizeToString(setupInfo.AvailableFileSize));
                                continue;
                            }

                            if (files.ContainsKey(file.ID.ToString()))
                            {
                                convertToExt = files[file.ID.ToString()];
                                if (!string.IsNullOrEmpty(convertToExt))
                                {
                                    newtitle = FileUtility.ReplaceFileExtension(path, convertToExt);
                                }
                            }
                        }

                        if (0 < counter)
                        {
                            var suffix = " (" + counter + ")";

                            if (!string.IsNullOrEmpty(entryId))
                            {
                                newtitle = 0 < newtitle.IndexOf('.') ? newtitle.Insert(newtitle.LastIndexOf('.'), suffix) : newtitle + suffix;
                            }
                            else
                            {
                                break;
                            }
                        }

                        zip.PutNextEntry(newtitle);

                        if (!string.IsNullOrEmpty(entryId) && file != null)
                        {
                            try
                            {
                                if (fileConverter.EnableConvert(file, convertToExt))
                                {
                                    //Take from converter
                                    using (var readStream = fileConverter.Exec(file, convertToExt))
                                    {
                                        readStream.StreamCopyTo(zip);
                                        if (!string.IsNullOrEmpty(convertToExt))
                                        {
                                            filesMessageService.Send(file, headers, MessageAction.FileDownloadedAs, file.Title, convertToExt);
                                        }
                                        else
                                        {
                                            filesMessageService.Send(file, headers, MessageAction.FileDownloaded, file.Title);
                                        }
                                    }
                                }
                                else
                                {
                                    using var readStream = FileDao.GetFileStream(file);
                                    readStream.StreamCopyTo(zip);
                                    filesMessageService.Send(file, headers, MessageAction.FileDownloaded, file.Title);
                                }
                            }
                            catch (Exception ex)
                            {
                                Error = ex.Message;
                                Logger.Error(Error, ex);
                            }
                        }
                        counter++;
                    }

                    ProgressStep();
                }
            }
            return(stream);
        }
        private Stream CompressToZip(NameValueCollection entries)
        {
            var stream = TempStream.Create();

            using (var zip = new ZipOutputStream(stream))
            {
                zip.IsStreamOwner = false;
                zip.SetLevel(3);
                zip.UseZip64 = UseZip64.Off;

                foreach (var title in entries.AllKeys)
                {
                    if (Canceled)
                    {
                        zip.Dispose();
                        stream.Dispose();
                        return(null);
                    }

                    var counter = 0;
                    foreach (var path in entries[title].Split(','))
                    {
                        var newtitle = title;
                        if (0 < counter)
                        {
                            var suffix = " (" + counter + ")";
                            newtitle = 0 < newtitle.IndexOf('.')
                                           ? newtitle.Insert(newtitle.LastIndexOf('.'), suffix)
                                           : newtitle + suffix;
                        }
                        var zipentry = new ZipEntry(newtitle)
                        {
                            DateTime = DateTime.UtcNow
                        };
                        lock (zip)
                        {
                            ZipConstants.DefaultCodePage = Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage;
                            zip.PutNextEntry(zipentry);
                        }
                        if (!string.IsNullOrEmpty(path))
                        {
                            var file = Global.DaoFactory.GetFileDao().GetFile(path);
                            if (file.ConvertedType != null)
                            {
                                //Take from converter
                                try
                                {
                                    using (var readStream = DocumentUtils.GetConvertedFile(file))
                                    {
                                        if (readStream != null)
                                        {
                                            readStream.StreamCopyTo(zip);
                                        }
                                    }
                                }
                                catch
                                {
                                }
                            }
                            else
                            {
                                using (var readStream = Global.DaoFactory.GetFileDao().GetFileStream(file))
                                {
                                    readStream.StreamCopyTo(zip);
                                }
                            }
                        }
                        counter++;
                    }
                    lock (zip)
                    {
                        ZipConstants.DefaultCodePage = Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage;
                        zip.CloseEntry();
                    }
                    ProgressStep();
                }
                return(stream);
            }
        }
        protected override void Do()
        {
            try
            {
                SetProgress((int?)MailOperationDownloadAllAttachmentsProgress.Init);

                CoreContext.TenantManager.SetCurrentTenant(CurrentTenant);

                try
                {
                    SecurityContext.AuthenticateMe(CurrentUser);
                }
                catch
                {
                    Error = Resource.SsoSettingsNotValidToken;
                    Logger.Error(Error);
                }

                var engine = new EngineFactory(CurrentTenant.TenantId, CurrentUser.ID.ToString());

                SetProgress((int?)MailOperationDownloadAllAttachmentsProgress.GetAttachments);

                var attachments =
                    engine.AttachmentEngine.GetAttachments(new ConcreteMessageAttachmentsExp(MessageId,
                                                                                             CurrentTenant.TenantId, CurrentUser.ID.ToString()));

                if (!attachments.Any())
                {
                    Error = MailCoreResource.NoAttachmentsInMessage;

                    throw new Exception(Error);
                }

                SetProgress((int?)MailOperationDownloadAllAttachmentsProgress.Zipping);

                var damagedAttachments = 0;

                using (var stream = TempStream.Create())
                {
                    using (var gzoStream = new GZipOutputStream(stream))
                        using (var gzip = new TarOutputStream(gzoStream, Encoding.UTF8))
                        {
                            gzoStream.IsStreamOwner = false;

                            var attachmentsCount = attachments.Count;
                            var progressMaxValue = (int)MailOperationDownloadAllAttachmentsProgress.ArchivePreparation;
                            var progressMinValue = (int)MailOperationDownloadAllAttachmentsProgress.Zipping;
                            var progresslength   = progressMaxValue - progressMinValue;
                            var progressStep     = (double)progresslength / attachmentsCount;
                            var zippingProgress  = 0.0;

                            var alreadyZipped = new List <string>();

                            foreach (var attachment in attachments)
                            {
                                var filename = attachment.fileName;

                                if (alreadyZipped.Contains(filename))
                                {
                                    var counter  = 1;
                                    var tempName = filename;
                                    while (alreadyZipped.Contains(tempName))
                                    {
                                        tempName = filename;
                                        var suffix = " (" + counter + ")";
                                        tempName = 0 < tempName.IndexOf('.')
                                        ? tempName.Insert(tempName.LastIndexOf('.'), suffix)
                                        : tempName + suffix;

                                        counter++;
                                    }
                                    filename = tempName;
                                }

                                alreadyZipped.Add(filename);

                                try
                                {
                                    using (var file = attachment.ToAttachmentStream())
                                    {
                                        ZipFile(gzip, filename, file.FileStream);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Logger.Error(ex);

                                    Error = string.Format(MailCoreResource.FileNotFoundOrDamaged, attachment.fileName);

                                    damagedAttachments++;
                                    using (var emptyStream = new MemoryStream())
                                    {
                                        ZipFile(gzip, filename, emptyStream); // Zip empty file
                                    }
                                }

                                zippingProgress += progressStep;

                                SetProgress(progressMinValue + (int?)zippingProgress);
                            }
                        }

                    SetProgress((int?)MailOperationDownloadAllAttachmentsProgress.ArchivePreparation);

                    if (stream.Length == 0)
                    {
                        Error = "File stream is empty";

                        throw new Exception(Error);
                    }

                    stream.Position = 0;

                    var store      = Global.GetStore();
                    var path       = string.Format(@"{0}\{1}", ((IAccount)Thread.CurrentPrincipal.Identity).ID, Defines.ARCHIVE_NAME);
                    var storedPath = store.Save(
                        FileConstant.StorageDomainTmp,
                        path,
                        stream,
                        MimeMapping.GetMimeMapping(path),
                        "attachment; filename=\"" + Defines.ARCHIVE_NAME + "\"");

                    Log.DebugFormat("Zipped archive has been stored to {0}", storedPath.ToString());
                }

                SetProgress((int?)MailOperationDownloadAllAttachmentsProgress.CreateLink);

                var baseDomain = CoreContext.Configuration.BaseDomain;

                var source = string.Format("{0}?{1}=bulk&{2}",
                                           "/Products/Files/HttpHandlers/filehandler.ashx",
                                           FilesLinkUtility.Action, "ext=.tar.gz");

                if (damagedAttachments > 1)
                {
                    Error = string.Format(MailCoreResource.FilesNotFound, damagedAttachments);
                }

                SetProgress((int?)MailOperationDownloadAllAttachmentsProgress.Finished, null, source);
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("Mail operation error -> Download all attachments: {0}", ex.ToString());
                Error = string.IsNullOrEmpty(Error)
                    ? "InternalServerError"
                    : Error;
            }
        }
        public Uri Save(string domain, string path, Stream stream, string contentType,
                        string contentDisposition, ACL acl, string contentEncoding = null, int cacheDays = 5,
                        DateTime?deleteAt = null, long?deleteAfter = null)
        {
            var buffered = stream.GetBuffered();

            if (QuotaController != null)
            {
                QuotaController.QuotaUsedCheck(buffered.Length);
            }

            var client = GetClient();

            var mime = string.IsNullOrEmpty(contentType)
                                 ? MimeMapping.GetMimeMapping(Path.GetFileName(path))
                                 : contentType;

            if (mime == "application/octet-stream")
            {
                contentDisposition = "attachment";
            }

            var customHeaders = new Dictionary <string, string>();

            if (cacheDays > 0)
            {
                customHeaders.Add("Cache-Control", string.Format("public, maxage={0}", (int)TimeSpan.FromDays(cacheDays).TotalSeconds));
                customHeaders.Add("Expires", DateTime.UtcNow.Add(TimeSpan.FromDays(cacheDays)).ToString());
            }

            if (deleteAt.HasValue)
            {
                var ts            = deleteAt.Value - new DateTime(1970, 1, 1, 0, 0, 0);
                var unixTimestamp = (long)ts.TotalSeconds;

                customHeaders.Add("X-Delete-At", unixTimestamp.ToString());
            }

            if (deleteAfter.HasValue)
            {
                customHeaders.Add("X-Delete-After", deleteAfter.ToString());
            }


            if (!string.IsNullOrEmpty(contentEncoding))
            {
                customHeaders.Add("Content-Encoding", contentEncoding);
            }

            var cannedACL = acl == ACL.Auto ? GetDomainACL(domain) : ACL.Read;

            if (cannedACL == ACL.Read)
            {
                try
                {
                    using (var emptyStream = TempStream.Create())
                    {
                        var headers = new Dictionary <string, string>
                        {
                            { "X-Object-Manifest", string.Format("{0}/{1}", _private_container, MakePath(domain, path)) }
                        };
                        // create symlink
                        client.CreateObject(_public_container,
                                            emptyStream,
                                            MakePath(domain, path),
                                            mime,
                                            4096,
                                            headers,
                                            _region
                                            );

                        emptyStream.Close();
                    }

                    client.PurgeObjectFromCDN(_public_container, MakePath(domain, path));
                }
                catch (Exception exp)
                {
                    _logger.InfoFormat("The invalidation {0} failed", _public_container + "/" + MakePath(domain, path));
                    _logger.Error(exp);
                }
            }
            stream.Position = 0;

            client.CreateObject(_private_container,
                                stream,
                                MakePath(domain, path),
                                mime,
                                4096,
                                customHeaders,
                                _region
                                );

            QuotaUsedAdd(domain, buffered.Length);

            return(GetUri(domain, path));
        }
Beispiel #22
0
        private Stream CompressTo(ItemNameValueCollection entriesPathId)
        {
            var stream = TempStream.Create();

            using (ICompress compressTo = new CompressToArchive(stream))
            {
                foreach (var path in entriesPathId.AllKeys)
                {
                    var counter = 0;
                    foreach (var entryId in entriesPathId[path])
                    {
                        if (CancellationToken.IsCancellationRequested)
                        {
                            compressTo.Dispose();
                            stream.Dispose();
                            CancellationToken.ThrowIfCancellationRequested();
                        }

                        var newtitle = path;

                        File file         = null;
                        var  convertToExt = string.Empty;

                        if (!string.IsNullOrEmpty(entryId))
                        {
                            FileDao.InvalidateCache(entryId);
                            file = FileDao.GetFile(entryId);

                            if (file == null)
                            {
                                Error = FilesCommonResource.ErrorMassage_FileNotFound;
                                continue;
                            }

                            if (files.ContainsKey(file.ID.ToString()))
                            {
                                convertToExt = files[file.ID.ToString()];
                                if (!string.IsNullOrEmpty(convertToExt))
                                {
                                    newtitle = FileUtility.ReplaceFileExtension(path, convertToExt);
                                }
                            }
                        }

                        if (0 < counter)
                        {
                            var suffix = " (" + counter + ")";

                            if (!string.IsNullOrEmpty(entryId))
                            {
                                newtitle = 0 < newtitle.IndexOf('.') ? newtitle.Insert(newtitle.LastIndexOf('.'), suffix) : newtitle + suffix;
                            }
                            else
                            {
                                break;
                            }
                        }

                        compressTo.CreateEntry(newtitle);

                        if (!string.IsNullOrEmpty(entryId) && file != null)
                        {
                            try
                            {
                                if (FileConverter.EnableConvert(file, convertToExt))
                                {
                                    //Take from converter
                                    using (var readStream = FileConverter.Exec(file, convertToExt))
                                    {
                                        compressTo.PutStream(readStream);

                                        if (!string.IsNullOrEmpty(convertToExt))
                                        {
                                            FilesMessageService.Send(file, headers, MessageAction.FileDownloadedAs, file.Title, convertToExt);
                                        }
                                        else
                                        {
                                            FilesMessageService.Send(file, headers, MessageAction.FileDownloaded, file.Title);
                                        }
                                    }
                                }
                                else
                                {
                                    using (var readStream = FileDao.GetFileStream(file))
                                    {
                                        compressTo.PutStream(readStream);

                                        FilesMessageService.Send(file, headers, MessageAction.FileDownloaded, file.Title);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                Error = ex.Message;
                                Logger.Error(Error, ex);
                            }
                        }
                        else
                        {
                            compressTo.PutNextEntry();
                        }
                        compressTo.CloseEntry();
                        counter++;
                    }
                    ProgressStep();
                }
            }
            return(stream);
        }
Beispiel #23
0
        private Stream CompressToZip(ItemNameValueCollection entriesPathId)
        {
            var stream = TempStream.Create();

            using (var zip = new ionic::Ionic.Zip.ZipOutputStream(stream, true))
            {
                zip.CompressionLevel       = ionic::Ionic.Zlib.CompressionLevel.Level3;
                zip.AlternateEncodingUsage = ionic::Ionic.Zip.ZipOption.AsNecessary;
                zip.AlternateEncoding      = Encoding.GetEncoding(Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage);

                foreach (var path in entriesPathId.AllKeys)
                {
                    if (Canceled)
                    {
                        zip.Dispose();
                        stream.Dispose();
                        return(null);
                    }

                    var counter = 0;
                    foreach (var entryId in entriesPathId[path])
                    {
                        var newtitle = path;

                        File file         = null;
                        var  convertToExt = string.Empty;

                        if (!string.IsNullOrEmpty(entryId))
                        {
                            file = FileDao.GetFile(entryId);

                            if (file.ContentLength > SetupInfo.AvailableFileSize)
                            {
                                Error = string.Format(FilesCommonResource.ErrorMassage_FileSizeZip, FileSizeComment.FilesSizeToString(SetupInfo.AvailableFileSize));
                                continue;
                            }

                            if (_files.ContainsKey(file.ID.ToString()))
                            {
                                if (_quotaDocsEdition)
                                {
                                    convertToExt = _files[file.ID.ToString()];
                                }

                                if (!string.IsNullOrEmpty(convertToExt))
                                {
                                    newtitle = FileUtility.ReplaceFileExtension(path, convertToExt);
                                }
                            }
                        }

                        if (0 < counter)
                        {
                            var suffix = " (" + counter + ")";

                            if (!string.IsNullOrEmpty(entryId))
                            {
                                newtitle = 0 < newtitle.IndexOf('.')
                                               ? newtitle.Insert(newtitle.LastIndexOf('.'), suffix)
                                               : newtitle + suffix;
                            }
                            else
                            {
                                break;
                            }
                        }

                        zip.PutNextEntry(newtitle);

                        if (!string.IsNullOrEmpty(entryId) && file != null)
                        {
                            if (file.ConvertedType != null || !string.IsNullOrEmpty(convertToExt))
                            {
                                //Take from converter
                                try
                                {
                                    using (var readStream = !string.IsNullOrEmpty(convertToExt)
                                                                ? FileConverter.Exec(file, convertToExt)
                                                                : FileConverter.Exec(file))
                                    {
                                        if (readStream != null)
                                        {
                                            readStream.StreamCopyTo(zip);
                                            if (!string.IsNullOrEmpty(convertToExt))
                                            {
                                                FilesMessageService.Send(file, httpRequestHeaders, MessageAction.FileDownloadedAs, file.Title, convertToExt);
                                            }
                                            else
                                            {
                                                FilesMessageService.Send(file, httpRequestHeaders, MessageAction.FileDownloaded, file.Title);
                                            }
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Error = ex.Message;

                                    Logger.Error(Error, ex);
                                }
                            }
                            else
                            {
                                using (var readStream = FileDao.GetFileStream(file))
                                {
                                    readStream.StreamCopyTo(zip);
                                    FilesMessageService.Send(file, httpRequestHeaders, MessageAction.FileDownloaded, file.Title);
                                }
                            }
                        }
                        counter++;
                    }

                    ProgressStep();
                }
                return(stream);
            }
        }
Beispiel #24
0
        protected override void Do()
        {
            try
            {
                SetProgress((int?)MailOperationDownloadAllAttachmentsProgress.Init);

                CoreContext.TenantManager.SetCurrentTenant(CurrentTenant);

                try
                {
                    SecurityContext.AuthenticateMe(CurrentUser);
                }
                catch
                {
                    Error = Resource.SsoSettingsNotValidToken;
                    Logger.Error(Error);
                }

                var engine = new EngineFactory(CurrentTenant.TenantId, CurrentUser.ID.ToString());

                SetProgress((int?)MailOperationDownloadAllAttachmentsProgress.GetAttachments);

                var attachments =
                    engine.AttachmentEngine.GetAttachments(new ConcreteMessageAttachmentsExp(MessageId,
                                                                                             CurrentTenant.TenantId, CurrentUser.ID.ToString()));

                if (!attachments.Any())
                {
                    Error = MailCoreResource.NoAttachmentsInMessage;

                    throw new Exception(Error);
                }

                SetProgress((int?)MailOperationDownloadAllAttachmentsProgress.Zipping);

                var damagedAttachments = 0;

                using (var stream = TempStream.Create())
                {
                    using (var zip = new ZipOutputStream(stream, true))
                    {
                        zip.CompressionLevel       = CompressionLevel.Level3;
                        zip.AlternateEncodingUsage = ZipOption.AsNecessary;
                        zip.AlternateEncoding      =
                            Encoding.GetEncoding(Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage);

                        var attachmentsCount = attachments.Count;
                        var progressMaxValue = (int)MailOperationDownloadAllAttachmentsProgress.ArchivePreparation;
                        var progressMinValue = (int)MailOperationDownloadAllAttachmentsProgress.Zipping;
                        var progresslength   = progressMaxValue - progressMinValue;
                        var progressStep     = (double)progresslength / attachmentsCount;
                        var zippingProgress  = 0.0;

                        foreach (var attachment in attachments)
                        {
                            try
                            {
                                using (var file = attachment.ToAttachmentStream())
                                {
                                    ZipFile(zip, file.FileName, file.FileStream);
                                }
                            }
                            catch (Exception ex)
                            {
                                Logger.Error(ex);

                                Error = string.Format(MailCoreResource.FileNotFoundOrDamaged, attachment.fileName);

                                damagedAttachments++;

                                ZipFile(zip, attachment.fileName); // Zip empty file
                            }

                            zippingProgress += progressStep;

                            SetProgress(progressMinValue + (int?)zippingProgress);
                        }
                    }

                    SetProgress((int?)MailOperationDownloadAllAttachmentsProgress.ArchivePreparation);

                    if (stream.Length == 0)
                    {
                        Error = "File stream is empty";

                        throw new Exception(Error);
                    }

                    stream.Position = 0;

                    var store = Global.GetStore();

                    var path = store.Save(
                        FileConstant.StorageDomainTmp,
                        string.Format(@"{0}\{1}", ((IAccount)Thread.CurrentPrincipal.Identity).ID, Defines.ARCHIVE_NAME),
                        stream,
                        "application/zip",
                        "attachment; filename=\"" + Defines.ARCHIVE_NAME + "\"");

                    Log.DebugFormat("Zipped archive has been stored to {0}", path.ToString());
                }

                SetProgress((int?)MailOperationDownloadAllAttachmentsProgress.CreateLink);

                var baseDomain = CoreContext.Configuration.BaseDomain;

                var source = string.Format("{0}?{1}=bulk",
                                           "/Products/Files/HttpHandlers/filehandler.ashx",
                                           FilesLinkUtility.Action);

                if (damagedAttachments > 1)
                {
                    Error = string.Format(MailCoreResource.FilesNotFound, damagedAttachments);
                }

                SetProgress((int?)MailOperationDownloadAllAttachmentsProgress.Finished, null, source);
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("Mail operation error -> Download all attachments: {0}", ex.ToString());
                Error = string.IsNullOrEmpty(Error)
                    ? "InternalServerError"
                    : Error;
            }
        }