예제 #1
0
        private void ApppendLogsTo(string logFileName, LogEventArg e)
        {
            if (logFileName.IsBlank())
            {
                return;
            }
            switch (e.Level)
            {
            case L4j.Off:                   //
            case L4j.Debug:                 //  ignore these levels
            case L4j.Trace: return;         //
            }
            if (e.ShowAs != ShowLogAs.Normal)
            {
                return;
            }

            if (_logFile == null)
            {
                _logFile = _fs.File(_fs.GetAssemblyDir().Bslash(logFileName));
            }

            var line = L.f + TextLog.Format(e.Title, e.Message);

            _logFile.Write(line, EncodeAs.UTF8, false, false);
        }
예제 #2
0
        public D7File_Out(FileShim fShim,
                          string serverFoldr,
                          bool isPrivate)
        {
            //if (!fShim.Found) Throw.Missing(fShim);
            var defLevl = fShim.DefaultLevel;

            fShim.DefaultLevel = L4j.Off;

            var fPath = isPrivate ? "private://" : "public://";

            fPath = fPath.Slash(serverFoldr).Slash(fShim.Name);

            this.file = new D7File
            {
                file     = fShim.ToBase64,
                filename = fShim.Name,
                filepath = fPath,
                //status   = Constants.FILE_STATUS_PERMANENT,
                //filemime = "application/x-msdos-program"
                //filemime = 149
            };

            fShim.DefaultLevel = defLevl;
        }
예제 #3
0
        public async Task<int> Post(FileShim file,
                                    CancellationToken cancelToken,
                                    string serverFoldr,
                                    bool isPrivate)
        {
            if (!IsLoggedIn)
                throw Error.BadAct($"‹{this.GetType().Name}› is not logged in.");

            Trace_n("Uploading file to server...", "");
            var req = _auth.Req.POST(URL.Api_FileJson);

            req.Body = new D7File_Out(file,
                            serverFoldr, isPrivate);

            D7File d7f = null; try
            {
                d7f = await _client.Send<D7File>(req, cancelToken, null,
                    "Successfully uploaded “{0}” ({1}) [fid: {2}].",
                        x => file.Name, x => file.Size.KB(), x => x.fid);
            }
            catch (RestServiceException ex) { OnFileUpload.Err(this, ex); }
            catch (Exception ex) { OnUnhandled.Err(this, ex); }


            if (d7f != null && d7f.fid > 0) return d7f.fid;
            else if (d7f == null) return Error_(-1, "Returned null.", "");
            else return Error_(-1, "Unexpected file id: " + d7f.fid, "");
        }
예제 #4
0
 public SessionAuth(IFileSystemShim fsShim, ISerializer serializer)
 {
     _fs         = fsShim;
     _serialzr   = serializer;
     var dir     = _fs.GetSpecialDir(SpecialDir.LocalApplicationData)
                                                 .Bslash(ParentDir);
     SessionFile = _fs.File(dir.Bslash("d7.session"));
 }
예제 #5
0
        private bool RelocateActiveFile(FileShim locF)
        {
            var newF = TempDir.Path.Bslash(locF.Name) + _suffix;

            if (!locF.MoveTo(newF))
                return Error_n("Unable to move currently in-use file.", locF.Path);
            else
                return true;
        }
예제 #6
0
        public SessionAuth(IFileSystemShim fsShim, ISerializer serializer)
        {
            _fs       = fsShim;
            _serialzr = serializer;
            var dir = _fs.GetSpecialDir(SpecialDir.LocalApplicationData)
                      .Bslash(ParentDir);

            SessionFile = _fs.File(dir.Bslash("d7.session"));
        }
예제 #7
0
        private void WriteCachedDate(string d7Changed)
        {
            if (_cachedDateFile == null)
            {
                _cachedDateFile = GetCachedDateFile();
            }

            _cachedDateFile.Write(d7Changed, raiseLogEvents: false);
        }
예제 #8
0
        public T Read <T>(FileShim fileShim, bool raiseLogEvents)
        {
            var s = raiseLogEvents ? fileShim.ReadUTF8
                                   : fileShim._ReadUTF8;

            if (s.IsBlank())
            {
                return(Warn_(default(T), "Invalid Json format.",
                             "Content of file is blank."));
            }
            return(this.Read <T>(s, raiseLogEvents));
        }
예제 #9
0
        private string ReadCachedDate()
        {
            if (_cachedDateFile == null)
            {
                _cachedDateFile = GetCachedDateFile();
            }

            if (!_cachedDateFile._Found)
            {
                return(null);
            }
            return(_cachedDateFile._ReadUTF8);
        }
예제 #10
0
        private bool RelocateActiveFile(FileShim locF)
        {
            var newF = TempDir.Path.Bslash(locF.Name) + _suffix;

            if (!locF.MoveTo(newF))
            {
                return(Error_n("Unable to move currently in-use file.", locF.Path));
            }
            else
            {
                return(true);
            }
        }
예제 #11
0
        private string ParseArguments(object[] args, out FileShim cacheFile)
        {
            if (args.Length == 0 || !(args[0] is string))
            {
                cacheFile = null;
                return(Error_("", $"Invalid argument for ‹{GetType().Name}›.LoadAsync()",
                              "LoadAsync(args[0]) should be the resource URL"));
            }
            var sufx = args.Length > 1 ? "_" + string.Join("_", args.Skip(1)) : "";

            cacheFile = DefineCacheFile(sufx);
            return(string.Join("/", args));;
        }
예제 #12
0
        public void Run(string batchFilePath)
        {
            File = _fs.File(batchFilePath);

            // if it's just a file name,
            //  - look for it beside the folder
            if (!File.Found)
            {
                var path = _fs.GetAssemblyDir().Bslash(batchFilePath);
                File = _fs.File(path);
            }

            ExecuteCmd(File);
        }
예제 #13
0
        public void Run(string batchFilePath)
        {
            File = _fs.File(batchFilePath);

            // if it's just a file name,
            //  - look for it beside the folder
            if (!File.Found)
            {
                var path = _fs.GetAssemblyDir().Bslash(batchFilePath);
                File = _fs.File(path);
            }

            ExecuteCmd(File);
        }
예제 #14
0
        /// <summary>
        /// Creates a temporary file in the system's temp folder.
        /// </summary>
        /// <param name="content"></param>
        /// <param name="encodeAs"></param>
        /// <returns></returns>
        public FileShim TempFile(string content, EncodeAs encodeAs = EncodeAs.UTF8)
        {
            string fPath; string errMsg; FileShim tempF = null;

            Debug_i("Creating temporary file...");

            if (!this.TryCreateNewTempFile(out fPath, out errMsg))
            {
                return(Error_o_(tempF, "Failed to create temporary file." + L.F + errMsg));
            }

            Debug_o("created: " + fPath);
            tempF = this.File(fPath);
            tempF.Write(content, encodeAs);
            return(tempF);
        }
예제 #15
0
        private void ApppendLogsTo(string logFileName, LogEventArg e)
        {
            if (logFileName.IsBlank()) return;
            switch (e.Level)
            {
                case L4j.Off:               //
                case L4j.Debug:             //  ignore these levels
                case L4j.Trace: return;     //
            }
            if (e.ShowAs != ShowLogAs.Normal) return;

            if (_logFile == null)
                _logFile = _fs.File(_fs.GetAssemblyDir().Bslash(logFileName));

            var line = L.f + TextLog.Format(e.Title, e.Message);
            _logFile.Write(line, EncodeAs.UTF8, false, false);
        }
예제 #16
0
        private bool SameAs(FileShim file, string json, string msg)
        {
            if (json.Length != file.ReadUTF8.Length)
            {
                RaiseDataChanged();
                return(Warn_n($"Different size for result: {json.Length.KB()}.", msg));
            }

            if (json.SHA1() == file.SHA1)
            {
                return(Debug_n("Resulting file is same as cached content.", msg));
            }
            else
            {
                RaiseDataChanged();
                return(Warn_n("SHA-1 checksum differed from cached file.", msg));
            }
        }
예제 #17
0
        protected T ReadAs <T>(string fileName) where T : IBasicAuthenticationKey
        {
            _file = _fs.File(_fs.GetAssemblyDir().Bslash(fileName));
            if (!_file.Found)
            {
                return(Warn_(default(T), $"Missing login file “{fileName}”.", _file.Path));
            }

            var ret = _serialr.Read <T>(_file);

            if (ret == null)
            {
                return(Error_(ret, $"Failed to parse ‹{typeof(T).Name}›.", ""));
            }

            UserName = ret.UserName;
            Password = ret.Password;
            BaseUrl  = ret.BaseUrl;

            return(ret);
        }
예제 #18
0
        private bool VerifyFile(FileShim actual, SyncableFileBase expctd)
        {
            var s = "Downloaded files is corrupted.";

            if (actual.Name != expctd.Name)
            {
                return(Error_n(s, $"expected name: {expctd.Name}  ==>  actual: {actual.Name}"));
            }

            if (actual.Size != expctd.Size)
            {
                return(Error_n(s, $"expected size: {expctd.Size.KB()}  ==>  actual: {actual.Size.KB()}"));
            }

            if (actual.SHA1 != expctd.SHA1)
            {
                return(Error_n(s, $"expected hash: {expctd.SHA1}  ==>  actual: {actual.SHA1}"));
            }

            return(true);
        }
예제 #19
0
        public D7File_Out(FileShim fShim,
                          string serverFoldr,
                          bool isPrivate)
        {
            //if (!fShim.Found) Throw.Missing(fShim);
            var defLevl = fShim.DefaultLevel;
            fShim.DefaultLevel = L4j.Off;

            var fPath = isPrivate ? "private://" : "public://";
            fPath = fPath.Slash(serverFoldr).Slash(fShim.Name);

            this.file = new D7File
            {
                file = fShim.ToBase64,
                filename = fShim.Name,
                filepath = fPath,
                //status   = Constants.FILE_STATUS_PERMANENT,
                //filemime = "application/x-msdos-program"
                //filemime = 149
            };

            fShim.DefaultLevel = defLevl;
        }
예제 #20
0
        private void ExecuteCmd(FileShim cmdFile)
        {
            Info_n("Running command from file...", cmdFile.Name);
            if (!cmdFile.Found)
            {
                Warn_n("Batch file not found: " + cmdFile.Name, cmdFile.Path);
                return;
            }

            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.Arguments = "/c " + cmdFile.Path;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            
            Info_n("Standard Output:", p.StandardOutput.ReadToEnd());
            Warn_n("Standard Error:", p.StandardError.ReadToEnd());

            p.WaitForExit();
        }
예제 #21
0
        private void ExecuteCmd(FileShim cmdFile)
        {
            Info_n("Running command from file...", cmdFile.Name);
            if (!cmdFile.Found)
            {
                Warn_n("Batch file not found: " + cmdFile.Name, cmdFile.Path);
                return;
            }

            Process p = new Process();

            p.StartInfo.FileName               = "cmd.exe";
            p.StartInfo.Arguments              = "/c " + cmdFile.Path;
            p.StartInfo.UseShellExecute        = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError  = true;
            p.StartInfo.CreateNoWindow         = true;
            p.Start();

            Info_n("Standard Output:", p.StandardOutput.ReadToEnd());
            Warn_n("Standard Error:", p.StandardError.ReadToEnd());

            p.WaitForExit();
        }
예제 #22
0
파일: Throw.cs 프로젝트: orf53975/ErrH
 public static void Missing(FileShim fileShim)
 {
     throw Error.MissingFile(fileShim.Path);
 }
예제 #23
0
        private bool VerifyFile(FileShim actual, SyncableFileBase expctd)
        {
            var s = "Downloaded files is corrupted.";

            if (actual.Name != expctd.Name)
                return Error_n(s, $"expected name: {expctd.Name}  ==>  actual: {actual.Name}");

            if (actual.Size != expctd.Size)
                return Error_n(s, $"expected size: {expctd.Size.KB()}  ==>  actual: {actual.Size.KB()}");

            if (actual.SHA1 != expctd.SHA1)
                return Error_n(s, $"expected hash: {expctd.SHA1}  ==>  actual: {actual.SHA1}");

            return true;
        }