Ejemplo n.º 1
0
        public static FileSystemWatcher OnFileChanged(this string filePath, Action onFileChanged)
        {
            if (filePath.IsBlank())
            {
                throw Fault.NullRef("filePath");
            }

            if (!File.Exists(filePath))
            {
                throw Fault.MissingFile(filePath);
            }

            var abs    = filePath.MakeAbsolute();
            var dir    = Path.GetDirectoryName(abs);
            var nme    = Path.GetFileName(abs);
            var watchr = new FileSystemWatcher(dir, nme);

            watchr.NotifyFilter = NotifyFilters.LastWrite;
            watchr.Changed     += (s, e) =>
            {
                try
                {
                    onFileChanged?.Invoke();
                }
                catch (Exception ex)
                {
                    Alert.Show(ex, "OnFileChanged");
                }
            };
            watchr.EnableRaisingEvents = true;
            return(watchr);
        }
Ejemplo n.º 2
0
        public virtual uint Insert(T newRecord)
        {
            if (newRecord == null)
            {
                throw Fault.NullRef <T>("record to insert");
            }

            SetStatus($"Inserting new ‹{TypeName}› record ...");
            BsonValue bVal;

            using (var db = ConnectToDB(out LiteCollection <T> col))
            {
                if (!PreInsertValidate(newRecord, col, out string msg))
                {
                    throw new InvalidDataException(msg);
                }

                using (var trans = db.BeginTrans())
                {
                    bVal = col.Insert(newRecord);

                    EnsureIndeces(col);
                    trans.Commit();
                }
            }
            var id = (uint)bVal.AsInt64;

            SetStatus($"Sucessfully inserted ‹{TypeName}› (id: {id}).");
            return(id);
        }
Ejemplo n.º 3
0
        public void RefreshStall(LeaseDTO lease)
        {
            if (Stalls == null)
            {
                return;
            }
            if (lease == null)
            {
                throw Fault.NullRef("Lease");
            }
            if (lease.Stall == null)
            {
                throw Fault.NullRef("Lease.Stall");
            }

            //lease.Stall = Stalls.Find(lease.Stall.Id, true);
            var stallID = lease.Stall.Id;

            if (_stalls.TryGetValue(stallID, out StallDTO cached))
            {
                lease.Stall = cached;
            }
            else
            {
                lease.Stall = Stalls.Find(stallID, true);
                //_stalls[stallID] = lease.Stall;
                try
                {
                    _stalls?.Add(stallID, lease.Stall);
                }
                catch { }
            }
        }
Ejemplo n.º 4
0
        private static async Task <IEnumerable <string> > ExtractArchive
            (string archivePath, string targetDir)
        {
            var zpr  = GetExtractor(archivePath);
            var tcs  = new TaskCompletionSource <IEnumerable <string> >();
            var list = new List <string>();

            zpr.FileExtractionFinished += (s, e)
                                          => list.Add(Chain(targetDir, e.FileInfo.FileName));

            zpr.ExtractionFinished += (s, e) => tcs.SetResult(list);

            zpr.ExtractArchive(targetDir);

            var contents = await tcs.Task;

            if (contents == null)
            {
                throw
                    Fault.NullRef <List <string> >("Extracted paths list");
            }

            if (contents.Count() == 0)
            {
                throw
                    Fault.NoMember("Archive did not contain any file.");
            }

            return(contents);
        }
Ejemplo n.º 5
0
        public static bool IsActive(this LeaseDTO lse, DateTime asOfDate)
        {
            asOfDate = asOfDate.Date;

            if (lse is InactiveLeaseDTO inactv)
            {
                if (asOfDate > inactv.DeactivatedDate)
                {
                    return(false);
                }
            }

            if (lse.ContractStart > asOfDate)
            {
                return(false);
            }
            if (lse.ContractEnd < asOfDate)
            {
                return(false);
            }

            if (lse.Stall == null)
            {
                throw Fault.NullRef("Lease.Stall");
            }
            if (!lse.Stall.IsOperational)
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 6
0
        private void InitializeFileWatcher()
        {
            if (WatchedFile.IsBlank())
            {
                throw Fault.NullRef(nameof(WatchedFile));
            }

            if (!File.Exists(WatchedFile))
            {
                throw Fault.MissingFile(WatchedFile);
            }

            var abs = WatchedFile.MakeAbsolute();
            var dir = Path.GetDirectoryName(abs);
            var nme = Path.GetFileName(abs);

            _watchr = new FileSystemWatcher(dir, nme);
            _watchr.NotifyFilter = NotifyFilters.LastWrite;
            _watchr.Changed     += async(s, e) =>
            {
                if (!_isDelaying)
                {
                    _isDelaying = true;
                    await Task.Delay(1000);

                    IsFileChanged = true;
                    OnFileChanged();
                    _isDelaying = false;
                }
            };
            _watchr.EnableRaisingEvents = true;
        }
Ejemplo n.º 7
0
        internal string ToAbsolute(string resourceURL)
        {
            if (Creds == null)
            {
                throw Fault.NullRef <R2Credentials>(nameof(Creds));
            }

            return(Creds.BaseURL.Slash(resourceURL));
        }
Ejemplo n.º 8
0
        public SharedLiteDB(string dbFilePath, string currentUser)
        {
            if (dbFilePath.IsBlank())
            {
                throw Fault.NullRef("DB File Path");
            }

            DbPath = dbFilePath;

            InitializeCommons(currentUser);

            //if (watchForChanges)
            //    InitializeFileWatcher();
        }
Ejemplo n.º 9
0
        public async Task <string> DownloadToTemp(R2PackagePart part, CancellationToken cancelTkn)
        {
            var byts = await _client.GetBytes <PartContentsByHash1>(cancelTkn, part.PartHash);

            if (byts == null)
            {
                throw Fault.NullRef <byte[]>("_client.GetBytes<PartContentsByHash1>");
            }
            if (byts.Length == 0)
            {
                throw Fault.NoItems("byte[] from _client.GetBytes<PartContentsByHash1>()");
            }

            return(_fileIO.WriteTempFile(byts));
        }
Ejemplo n.º 10
0
        public SharedLiteDB(string dbFilePath, string currentUser)
        {
            if (dbFilePath.IsBlank())
            {
                throw Fault.NullRef("DB File Path");
            }

            DbPath = dbFilePath;

            InitializeCommons(currentUser);

            //if (!File.Exists(DbPath))
            //    Metadata.CreateInitialRecord();

            InitializeFileWatcher();
        }
Ejemplo n.º 11
0
        public SharedLiteDB(string dbFilePath, string currentUser, bool doInitialize = true)
        {
            if (dbFilePath.IsBlank())
            {
                throw Fault.NullRef("DB File Path");
            }

            DbPath = dbFilePath;

            InitializeCommons(currentUser);
            if (doInitialize)
            {
                InitializeCollections();
            }

            if (!File.Exists(DbPath))
            {
                Metadata.CreateInitialRecord();
            }

            InitializeFileWatcher();
        }
Ejemplo n.º 12
0
        private int GetNodeID(Dictionary <string, object> dict)
        {
            if (Result == null)
            {
                throw
                    Fault.NullRef <Dictionary <string, object> >(nameof(Result));
            }

            object obj;

            if (!Result.TryGetValue("nid", out obj))
            {
                throw Fault.NoMember("nid");
            }

            if (obj == null)
            {
                throw Fault.NullRef <object>("nid");
            }

            var json = obj.ToString();

            if (json.IsBlank())
            {
                throw Fault.BlankText("json nid");
            }

            json = json.Replace("\"", "");

            var numbr = json.Between("[{value:", "}]");

            if (!numbr.IsNumeric())
            {
                throw Fault.BadCast <int>(numbr);
            }

            return(numbr.ToInt());
        }