Esempio n. 1
0
        public override async Task <Stream> OpenStreamAsync(IEntryModel entryModel,
                                                            FileExplorer.Defines.FileAccess access, CancellationToken ct)
        {
            //SevenZipWrapper wrapper = (Profile as SzsProfile).Wrapper;
            ISzsItemModel entryItemModel = entryModel as ISzsItemModel;

            //IEntryModel rootReferenceModel = itemModel.Root.ReferencedFile;
            //return new CompressMemoryStream(wrapper, rootReferenceModel, itemModel.RelativePath, access, ct);
            //To-DO: save to Profile.DiskIO.Mapper[itemModel].IOPath


            if (entryItemModel.Root.Equals(entryItemModel))
            {
                IEntryModel referencedFile = entryItemModel.Root.ReferencedFile;
                return(await(referencedFile.Profile as IDiskProfile).DiskIO.OpenStreamAsync(referencedFile, access, ct));
            }

            switch (access)
            {
            case FileExplorer.Defines.FileAccess.Read: return(await SzsFileStream.OpenReadAsync(entryModel, ct));

            case FileExplorer.Defines.FileAccess.Write: return(SzsFileStream.OpenWrite(entryModel));

            case FileExplorer.Defines.FileAccess.ReadWrite: return(await SzsFileStream.OpenReadWriteAsync(entryModel, ct));
            }
            throw new NotSupportedException();
        }
Esempio n. 2
0
        public override async Task <IEntryModel> RenameAsync(IEntryModel entryModel, string newName, CancellationToken ct)
        {
            SevenZipWrapper wrapper  = (Profile as SzsProfile).Wrapper;
            string          destPath = Profile.Path.Combine(Profile.Path.GetDirectoryName(entryModel.FullPath), newName);

            SzsProfile    profile       = Profile as SzsProfile;
            ISzsItemModel szsEntryModel = entryModel as ISzsItemModel;
            string        type          = profile.Path.GetExtension(szsEntryModel.Root.Name);

            using (var releaser = await profile.WorkingLock.LockAsync())
                using (var stream = await profile.DiskIO.OpenStreamAsync(szsEntryModel.Root, Defines.FileAccess.ReadWrite, ct))
                    wrapper.Modify(type, stream, (entryModel as ISzsItemModel).RelativePath, newName,
                                   entryModel.IsDirectory && !(entryModel is SzsRootModel));

            lock (profile.VirtualModels)
            {
                if (profile.VirtualModels.Contains(szsEntryModel))
                {
                    profile.VirtualModels.Remove(szsEntryModel);
                }
            }

            Profile.NotifyEntryChanges(this, destPath, Defines.ChangeType.Moved, entryModel.FullPath);

            return(await Profile.ParseAsync(destPath));
        }
Esempio n. 3
0
        public override async Task DeleteAsync(IEntryModel entryModel, CancellationToken ct)
        {
            SzsProfile    profile       = Profile as SzsProfile;
            ISzsItemModel szsEntryModel = entryModel as ISzsItemModel;

            if (szsEntryModel is SzsRootModel)
            {
                IEntryModel rootFile = (szsEntryModel as SzsRootModel).ReferencedFile;
                await(rootFile.Profile as IDiskProfile)
                .DiskIO.DeleteAsync(rootFile, ct);
                return;
            }

            using (var releaser = await profile.WorkingLock.LockAsync())
                using (var stream = await profile.DiskIO.OpenStreamAsync(szsEntryModel.Root, Defines.FileAccess.ReadWrite, ct))
                {
                    string type = profile.Path.GetExtension(szsEntryModel.Root.Name);

                    profile.Wrapper.Delete(type, stream, szsEntryModel.RelativePath + (szsEntryModel.IsDirectory ? "\\*" : ""));

                    lock (profile.VirtualModels)
                        if (profile.VirtualModels.Contains(szsEntryModel))
                        {
                            profile.VirtualModels.Remove(szsEntryModel);
                        }
                }
        }
Esempio n. 4
0
 /// <summary>
 /// Not Serializable, transfer source entry to destentry.
 /// For transfer to ISzsItemModel only, if unsure destination use DiskTransfer with allowCustom on.
 /// </summary>
 /// <param name="srcModel"></param>
 /// <param name="destDirModel"></param>
 /// <param name="removeOriginal"></param>
 /// <param name="allowCustomImplementation"></param>
 /// <param name="nextCommand"></param>
 /// <returns></returns>
 public static IScriptCommand SzsDiskTransfer(IEntryModel[] srcModels, ISzsItemModel destDirModel, bool removeOriginal = false,
                                              IScriptCommand nextCommand = null)
 {
     return(ScriptCommands.Assign("{SourceDiskTransferEntry}", srcModels, false,
                                  ScriptCommands.Assign("{DestinationDiskTransferEntry}", destDirModel, false,
                                                        SzsDiskTransfer("{SourceDiskTransferEntry}", "{DestinationDiskTransferEntry}",
                                                                        removeOriginal, nextCommand))));
 }
Esempio n. 5
0
 public SzsDiskIOHelper(SzsProfile profile)
     : base(profile)
 {
     this.Mapper = new FileBasedDiskPathMapper(m =>
     {
         ISzsItemModel model = m as ISzsItemModel;
         return(model.Profile.Path.Combine(model.Root.FullPath, model.RelativePath));
     });
 }
Esempio n. 6
0
        public override async Task <IEntryModel> CreateAsync(string fullPath, bool isDirectory, CancellationToken ct)
        {
            string        parentPath = Profile.Path.GetDirectoryName(fullPath);
            string        name       = Profile.Path.GetFileName(fullPath);
            ISzsItemModel parentDir  = await Profile.ParseAsync(parentPath) as ISzsItemModel;

            if (parentDir == null)
            {
                throw new Exception(String.Format("Parent dir {0} not exists.", parentPath));
            }
            string        relativePath  = Profile.Path.Combine(parentDir.RelativePath, name);
            ISzsItemModel retEntryModel = new SzsChildModel(parentDir.Root, relativePath, isDirectory);

            (Profile as SzsProfile).VirtualModels.Add(retEntryModel);
            return(retEntryModel);
        }
Esempio n. 7
0
        public static async Task <WebFileStream> OpenReadAsync(IEntryModel entryModel, CancellationToken ct)
        {
            var           profile        = entryModel.Profile as SzsProfile;
            ISzsItemModel entryItemModel = entryModel as ISzsItemModel;
            IEntryModel   rootModel      = entryItemModel.Root.ReferencedFile;

            byte[] bytes = new byte[] { };
            using (Stream stream = await(rootModel.Profile as IDiskProfile).DiskIO.OpenStreamAsync(rootModel, Defines.FileAccess.Read, ct))
            {
                MemoryStream ms = new MemoryStream();
                if (profile.Wrapper.ExtractOne(stream, entryItemModel.RelativePath, null, ms))
                {
                    bytes = ms.ToByteArray();
                }
            }

            return(new WebFileStream(entryModel, bytes, null));
        }
Esempio n. 8
0
        public override async Task <IScriptCommand> ExecuteAsync(ParameterDic pm)
        {
            Dictionary <string, Stream> compressDic = new Dictionary <string, Stream>();

            try
            {
                IEntryModel[] srcEntries = await pm.GetValueAsEntryModelArrayAsync(SourceEntryKey);

                ISzsItemModel destEntry = await pm.GetValueAsEntryModelAsync(DestinationDirectoryEntryKey, null) as ISzsItemModel;

                //If destination is not SzsRoot, use DiskTransfer instead.
                SzsProfile destProfile = destEntry.Profile as SzsProfile;
                if (destProfile == null)
                {
                    logger.Warn(String.Format("{0} isn't Szs based entry, DiskTransfer is used instead.", destEntry.Name));
                    return(IOScriptCommands.DiskTransfer(SourceEntryKey, DestinationDirectoryEntryKey, null, RemoveOriginal, false, NextCommand));
                }
                if (!destEntry.IsDirectory)
                {
                    return(ResultCommand.Error(new ArgumentException(DestinationDirectoryEntryKey + " is not a folder.")));
                }

                Func <IEntryModel, bool> fileAndArchiveOnly = em => !em.IsDirectory || (em is SzsRootModel);
                Func <IEntryModel, bool> lookupDirectoryNotArchiveFilter = em => em.IsDirectory && !(em is SzsRootModel);

                IProgress <TransferProgress> progress = pm.GetProgress();

                string archiveType = destProfile.Path.GetExtension((destEntry as ISzsItemModel).Root.Name);
                logger.Info(String.Format("Compressing {0} -> {1} using SzsDiskTransfer",
                                          srcEntries.GetDescription(), destEntry.Name));

                await Task.Run(async() =>
                {
                    #region OpenStream of files
                    foreach (var srcEntry in srcEntries)
                    {
                        IDiskProfile srcProfile = srcEntry.Profile as IDiskProfile;
                        if (srcProfile == null)
                        {
                            break;
                        }


                        if (fileAndArchiveOnly(srcEntry))
                        {
                            logger.Debug(String.Format("Added to Dictionary : {0} -> {1}", srcEntry.FullPath, srcEntry.Name));
                            progress.Report(TransferProgress.SetMessage(ProgressType.Running, srcEntry.Name));
                            compressDic.Add(srcEntry.Name, await srcProfile.DiskIO
                                            .OpenStreamAsync(srcEntry, Defines.FileAccess.Read, pm.CancellationToken));
                        }
                        else
                        {
                            IList <IEntryModel> srcSubEntries = await srcProfile.ListRecursiveAsync(srcEntry, pm.CancellationToken,
                                                                                                    fileAndArchiveOnly, lookupDirectoryNotArchiveFilter, false);

                            foreach (var srcSubEntry in srcSubEntries)
                            {
                                string relativePath =
                                    destProfile.Path.Combine(
                                        destEntry.RelativePath,
                                        srcSubEntry.FullPath.Replace(srcEntry.Parent.FullPath, "").TrimStart('\\')
                                        );
                                logger.Debug(String.Format("Added to Dictionary : {0} -> {1}", srcSubEntry.FullPath, relativePath));
                                progress.Report(TransferProgress.SetMessage(ProgressType.Running, relativePath));
                                compressDic.Add(relativePath, await srcProfile.DiskIO
                                                .OpenStreamAsync(srcSubEntry, Defines.FileAccess.Read, pm.CancellationToken));
                            }
                        }
                    }
                    #endregion

                    Progress <Defines.ProgressEventArgs> progress1 = new Progress <Defines.ProgressEventArgs>(
                        (pea) =>
                    {
                        if (!String.IsNullOrEmpty(pea.Message))
                        {
                            progress.Report(TransferProgress.SetMessage(Defines.ProgressType.Running, pea.Message));
                        }
                        if (!String.IsNullOrEmpty(pea.File))
                        {
                            progress.Report(TransferProgress.From(pea.File));
                        }
                        if (pea.CurrentProgress != -1 && pea.TotalProgress != -1)
                        {
                            progress.Report(TransferProgress.UpdateCurrentProgress((short)((float)pea.CurrentProgress / (float)pea.TotalProgress * 100.0)));
                        }
                    }
                        );

                    progress.Report(TransferProgress.To(destEntry.Name));
                    using (await destProfile.WorkingLock.LockAsync())
                        using (var stream = await destProfile.DiskIO.OpenStreamAsync(destEntry, Defines.FileAccess.ReadWrite, pm.CancellationToken))
                            destProfile.Wrapper.CompressMultiple(archiveType, stream, compressDic, progress1);

                    logger.Info(String.Format("{0} items transfered", compressDic.Count()));
                    return(CoreScriptCommands.NotifyEntryChanged(ChangeType.Changed, destEntry, NextCommand));
                });


                return(NextCommand);
            }
            finally
            {
                #region Dispose Streams
                if (compressDic != null)
                {
                    foreach (var stream in compressDic.Values)
                    {
                        stream.Dispose();
                    }
                }
                #endregion
            }
        }