public void Add(ItemNameValueCollection <T> collection) { foreach (var key in collection.AllKeys) { foreach (var value in collection[key]) { Add(key, value); } } }
private static void ReplaceLongPath(ItemNameValueCollection entriesPathId) { foreach (var path in new List <string>(entriesPathId.AllKeys)) { if (200 >= path.Length || 0 >= path.IndexOf('/')) { continue; } var ids = entriesPathId[path]; entriesPathId.Remove(path); var newtitle = "LONG_FOLDER_NAME" + path.Substring(path.LastIndexOf('/')); entriesPathId.Add(newtitle, ids); } }
private ItemNameValueCollection GetEntriesPathId() { var entriesPathId = new ItemNameValueCollection(); if (0 < Files.Count) { var files = FileDao.GetFiles(Files.ToArray()); files = FilesSecurity.FilterRead(files).ToList(); files.ForEach(file => entriesPathId.Add(ExecPathFromFile(file, string.Empty))); } if (0 < Folders.Count) { FilesSecurity.FilterRead(FolderDao.GetFolders(Files.ToArray())).ToList().Cast <FileEntry>().ToList() .ForEach(folder => FileMarker.RemoveMarkAsNew(folder)); var filesInFolder = GetFilesInFolders(Folders, string.Empty); entriesPathId.Add(filesInFolder); } return(entriesPathId); }
private ItemNameValueCollection GetFilesInFolders(IEnumerable <object> folderIds, string path) { if (Canceled) { return(null); } var entriesPathId = new ItemNameValueCollection(); foreach (var folderId in folderIds) { var folder = FolderDao.GetFolder(folderId); if (folder == null || !FilesSecurity.CanRead(folder)) { continue; } var folderPath = path + folder.Title + "/"; var files = FolderDao.GetFiles(folder.ID, null, FilterType.None, Guid.Empty, string.Empty); files = FilesSecurity.FilterRead(files).ToList(); files.ForEach(file => entriesPathId.Add(ExecPathFromFile(file, folderPath))); FileMarker.RemoveMarkAsNew(folder); var nestedFolders = FolderDao.GetFolders(folder.ID); nestedFolders = FilesSecurity.FilterRead(nestedFolders).ToList(); if (files.Count == 0 && nestedFolders.Count == 0) { entriesPathId.Add(folderPath, String.Empty); } var filesInFolder = GetFilesInFolders(nestedFolders.ConvertAll(f => f.ID), folderPath); if (filesInFolder == null) { return(null); } entriesPathId.Add(filesInFolder); } return(entriesPathId); }
private ItemNameValueCollection ExecPathFromFile(File file, string path) { FileMarker.RemoveMarkAsNew(file); var title = file.Title; if (files.ContainsKey(file.ID.ToString())) { var convertToExt = files[file.ID.ToString()]; if (!string.IsNullOrEmpty(convertToExt)) { title = FileUtility.ReplaceFileExtension(title, convertToExt); } } var entriesPathId = new ItemNameValueCollection(); entriesPathId.Add(path + title, file.ID.ToString()); return(entriesPathId); }
private ItemNameValueCollection GetFilesInFolders(IServiceScope scope, IEnumerable <object> folderIds, string path) { var fileMarker = scope.ServiceProvider.GetService <FileMarker>(); CancellationToken.ThrowIfCancellationRequested(); var entriesPathId = new ItemNameValueCollection(); foreach (var folderId in folderIds) { CancellationToken.ThrowIfCancellationRequested(); var folder = FolderDao.GetFolder(folderId); if (folder == null || !FilesSecurity.CanRead(folder)) { continue; } var folderPath = path + folder.Title + "/"; var files = FileDao.GetFiles(folder.ID, null, FilterType.None, false, Guid.Empty, string.Empty, true); files = FilesSecurity.FilterRead(files).ToList(); files.ForEach(file => entriesPathId.Add(ExecPathFromFile(scope, file, folderPath))); fileMarker.RemoveMarkAsNew(folder); var nestedFolders = FolderDao.GetFolders(folder.ID); nestedFolders = FilesSecurity.FilterRead(nestedFolders).ToList(); if (files.Count == 0 && nestedFolders.Count == 0) { entriesPathId.Add(folderPath, string.Empty); } var filesInFolder = GetFilesInFolders(scope, nestedFolders.ConvertAll(f => f.ID), folderPath); entriesPathId.Add(filesInFolder); } return(entriesPathId); }
private ItemNameValueCollection GetEntriesPathId(out List <File> filesForSend, out List <Folder> folderForSend) { filesForSend = new List <File>(); folderForSend = new List <Folder>(); var entriesPathId = new ItemNameValueCollection(); if (0 < Files.Count) { filesForSend = FilesSecurity.FilterDownload(FileDao.GetFiles(Files)); filesForSend.ForEach(file => entriesPathId.Add(ExecPathFromFile(file, string.Empty))); } if (0 < Folders.Count) { folderForSend = FolderDao.GetFolders(Folders); folderForSend = FilesSecurity.FilterDownload(folderForSend); folderForSend.ForEach(folder => FileMarker.RemoveMarkAsNew(folder)); var filesInFolder = GetFilesInFolders(folderForSend.Select(x => x.ID), string.Empty); entriesPathId.Add(filesInFolder); } if (Folders.Count == 1 && Files.Count == 0) { var entriesPathIdWithoutRoot = new ItemNameValueCollection(); foreach (var path in entriesPathId.AllKeys) { entriesPathIdWithoutRoot.Add(path.Remove(0, path.IndexOf('/') + 1), entriesPathId[path]); } return(entriesPathIdWithoutRoot); } return(entriesPathId); }
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); } }
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 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); }