Ejemplo n.º 1
0
        /*
         * WB082 Behavior
         * ExtractFile : DestDir must be Directory, create if not exists.
         * Ex) (...),README.txt,%BaseDir%\Temp\Hello
         *   -> No Hello : Create direcotry "Hello" and extract files into new directory.
         *   -> Hello is a file : Failure
         *   -> Hello is a directory : Extract files into directory.
         *
         * ExtractAllFiles
         * Ex) (...),Fonts,%BaseDir%\Temp\Hello
         *   -> No Hello : Failure
         *   -> Hello is a file : Failure
         *   -> Hello is a direcotry : Extract files into directory.
         *
         */

        public static List <LogInfo> ExtractFile(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>();

            Debug.Assert(cmd.Info.GetType() == typeof(CodeInfo_ExtractFile));
            CodeInfo_ExtractFile info = cmd.Info as CodeInfo_ExtractFile;

            string pluginFile = StringEscaper.Preprocess(s, info.PluginFile);
            string dirName    = StringEscaper.Preprocess(s, info.DirName);
            string fileName   = StringEscaper.Preprocess(s, info.FileName);
            string destDir    = StringEscaper.Preprocess(s, info.DestDir); // Should be directory name

            Plugin p = Engine.GetPluginInstance(s, cmd, s.CurrentPlugin.FullPath, pluginFile, out bool inCurrentPlugin);

            if (StringEscaper.PathSecurityCheck(destDir, out string errorMsg) == false)
            {
                logs.Add(new LogInfo(LogState.Error, errorMsg));
                return(logs);
            }

            if (!Directory.Exists(destDir)) // DestDir already exists
            {
                if (File.Exists(destDir))   // Error, cannot proceed
                {
                    logs.Add(new LogInfo(LogState.Error, $"File [{destDir}] is not a directory."));
                    return(logs);
                }
                else
                {
                    Directory.CreateDirectory(destDir);
                }
            }

            string destPath = Path.Combine(destDir, fileName);

            using (MemoryStream ms = EncodedFile.ExtractFile(p, dirName, fileName))
                using (FileStream fs = new FileStream(destPath, FileMode.Create, FileAccess.Write))
                {
                    ms.Position = 0;
                    ms.CopyTo(fs);
                }

            logs.Add(new LogInfo(LogState.Success, $"Encoded file [{fileName}] extracted to [{destDir}]"));

            return(logs);
        }
Ejemplo n.º 2
0
        /*
         * WB082 Behavior
         * ExtractFile : DestDir must be Directory, create if not exists.
         * Ex) (...),README.txt,%BaseDir%\Temp\Hello
         *   -> No Hello : Create directory "Hello" and extract files into new directory.
         *   -> Hello is a file : Failure
         *   -> Hello is a directory : Extract files into directory.
         *
         * ExtractAllFiles
         * Ex) (...),Fonts,%BaseDir%\Temp\Hello
         *   -> No Hello : Failure
         *   -> Hello is a file : Failure
         *   -> Hello is a directory : Extract files into directory.
         *
         * PEBakery Behavior
         * ExtractFile/ExtractAllFiles : DestDir must be Directory, create if not exists.
         * Ex) (...),README.txt,%BaseDir%\Temp\Hello
         *   -> No Hello : Create directory "Hello" and extract files into new directory.
         *   -> Hello is a file : Failure
         *   -> Hello is a directory : Extract files into directory.
         */

        public static List <LogInfo> ExtractFile(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>();

            CodeInfo_ExtractFile info = cmd.Info.Cast <CodeInfo_ExtractFile>();

            string scriptFile = StringEscaper.Preprocess(s, info.ScriptFile);
            string dirName    = StringEscaper.Preprocess(s, info.DirName);
            string fileName   = StringEscaper.Preprocess(s, info.FileName);
            string destDir    = StringEscaper.Preprocess(s, info.DestDir); // Should be directory name

            Script sc = Engine.GetScriptInstance(s, s.CurrentScript.RealPath, scriptFile, out _);

            // Check if encoded file exist
            if (!EncodedFile.ContainsFile(sc, dirName, fileName))
            {
                return(LogInfo.LogErrorMessage(logs, $"Encoded file [{dirName}\\{fileName}] not found in script [{sc.RealPath}]."));
            }

            // Filter dest path
            if (!StringEscaper.PathSecurityCheck(destDir, out string errorMsg))
            {
                return(LogInfo.LogErrorMessage(logs, errorMsg));
            }

            if (!Directory.Exists(destDir)) // DestDir already exists
            {
                if (File.Exists(destDir))   // Error, cannot proceed
                {
                    return(LogInfo.LogErrorMessage(logs, $"File [{destDir}] is not a directory."));
                }

                Directory.CreateDirectory(destDir);
            }

            s.MainViewModel.SetBuildCommandProgress("ExtractFile Progress", 1);
            try
            {
                object             progressLock = new object();
                IProgress <double> progress     = new Progress <double>(x =>
                {
                    lock (progressLock)
                    {
                        s.MainViewModel.BuildCommandProgressValue = x;
                        if (x < EncodedFile.Base64ReportFactor)
                        { // [Stage 1] Base64
                            s.MainViewModel.BuildCommandProgressText = $"Reading \"{fileName}\" from script\r\n({x * 100:0.0}%)";
                        }
                        else
                        { // [Stage 2] Decompress
                            s.MainViewModel.BuildCommandProgressText = $"Decompressing \"{fileName}\"\r\n({x * 100:0.0}%)";
                        }
                    }
                });

                string destPath = Path.Combine(destDir, fileName);
                using (FileStream fs = new FileStream(destPath, FileMode.Create, FileAccess.Write))
                {
                    EncodedFile.ExtractFile(sc, dirName, fileName, fs, progress);
                }
            }
            finally
            {
                s.MainViewModel.ResetBuildCommandProgress();
            }

            logs.Add(new LogInfo(LogState.Success, $"Encoded file [{fileName}] was extracted to [{destDir}]"));

            return(logs);
        }