예제 #1
0
        private async Task CheckForDeletedFiles(BackedUpDirectory directory, string[] existingFiles)
        {
            List <string> existingFileNames = new List <string>(existingFiles.Length);

            foreach (var filePath in existingFiles)
            {
                existingFileNames.Add(Path.GetFileName(filePath));
            }
            var filesToDelete = new List <BackedUpFile>();

            foreach (var backedUpFile in directory.Files)
            {
                if (!existingFileNames.Contains(backedUpFile.Name))
                {
                    filesToDelete.Add(backedUpFile);
                }
            }
            if (filesToDelete.Count > 0)
            {
                Console.WriteLine($"Deleting: {string.Join(", ", filesToDelete)}");
                var result = await GetFromPost($"{_serverUri}/{WebApi.DeleteFiles}",
                                               JsonConvert.SerializeObject(filesToDelete), typeof(bool))
                             .ConfigureAwait(false);
            }
        }
예제 #2
0
        private async Task <BackedUpFile> UpdateFile(BackedUpDirectory directory, string filePath)
        {
            var fileName     = Path.GetFileName(filePath);
            var backedUpFile = directory.Files?.FirstOrDefault(f => f.Name == fileName);

            if (backedUpFile == null)
            {
                backedUpFile = new BackedUpFile()
                {
                    Name     = fileName,
                    Modified = Directory.GetLastWriteTimeUtc(filePath),
                    ParentId = directory.Id
                };
                Console.WriteLine($"Uploading new file {fileName}");
                // Upload file and object, link object to parent
                var result = await Upload(filePath, backedUpFile)
                             .ConfigureAwait(false);

                backedUpFile = JsonConvert.DeserializeObject <BackedUpFile>(result);
            }
            else
            {
                var lastWrite = Directory.GetLastWriteTimeUtc(filePath);
                if (lastWrite > backedUpFile.Modified)
                {
                    Console.WriteLine($"Uploading modified file {fileName}");
                    backedUpFile.Modified = lastWrite;
                    var result = await Upload(filePath, backedUpFile)
                                 .ConfigureAwait(false);

                    backedUpFile = JsonConvert.DeserializeObject <BackedUpFile>(result);
                }
            }
            return(backedUpFile);
        }
예제 #3
0
        public void AddDirAndFile()
        {
            var dir = new BackedUpDirectory()
            {
                Modified = DateTime.Now,
                Name     = @"c:\root1"
            };

            _dbService.AddDirectory(dir);

            var file1 = new BackedUpFile()
            {
                Name     = "file1",
                ParentId = dir.Id,
                Modified = DateTime.Now
            };

            _dbService.AddFile(file1);

            var dir2 = _dbService.GetDirectory(@"c:\root1");

            Assert.True(dir.Equals(dir2));
            var file2 = _dbService.GetFile("file1", dir.Id);

            Assert.True(file1.Id == file2.Id);
        }
예제 #4
0
        public void AddThreeDirsAFileAndCheckDeps()
        {
            var grandparent = new BackedUpDirectory()
            {
                Name = "vol:", Depth = 1
            };
            var parent = new BackedUpDirectory {
                Name = "parent", Depth = 2
            };
            var child = new BackedUpDirectory {
                Name = "child", Depth = 3
            };
            var parentFile = new BackedUpFile {
                Name = "parentFile"
            };

            _dbService.AddDirectory(grandparent);
            Assert.Null(grandparent.Parent);

            parent.ParentId = grandparent.Id;
            _dbService.AddDirectory(parent);
            Assert.True(parent.ParentId == grandparent.Id);

            child.ParentId = parent.Id;
            _dbService.AddDirectory(child);
            Assert.True(child.ParentId == parent.Id);

            parentFile.ParentId = parent.Id;
            _dbService.AddFile(parentFile);

            var dir = _dbService.GetDirectory(@"vol:/parent");

            Assert.True(dir.Directories.Count == 0);
            Assert.True(dir.Files.Count == 0);

            dir = _dbService.GetDirectory(@"vol:/parent", includeChildren: true);
            Assert.True(dir.Directories.Count == 1);
            Assert.True(dir.Files.Count == 1);

            dir = _dbService.GetDirectory(parent.Id);
            Assert.True(dir.Directories.Count == 0);
            Assert.True(dir.Files.Count == 0);
            Assert.Null(dir.Parent);

            dir = _dbService.GetDirectory(parent.Id, includeChildren: true);
            Assert.True(dir.Directories.Count == 1);
            Assert.True(dir.Files.Count == 1);
            Assert.Null(dir.Parent);

            dir = _dbService.GetDirectory(parent.Id, includeParent: true);
            Assert.True(dir.Directories.Count == 0);
            Assert.True(dir.Files.Count == 0);
            Assert.True(dir.Parent.Id == parent.ParentId);

            dir = _dbService.GetDirectory(child.Name, parent.Id);
            Assert.True(dir.Id == child.Id);
            Assert.True(dir.Name == child.Name);
            Assert.True(dir.ParentId == parent.Id);
        }
예제 #5
0
        private async Task <BackedUpDirectory> UpdateDirectory(BackedUpDirectory directory)
        {
            directory = await GetFromPost($"{_serverUri}/{WebApi.UpdateDirectory}",
                                          JsonConvert.SerializeObject(directory), typeof(BackedUpDirectory))
                        .ConfigureAwait(false) as BackedUpDirectory;

            return(directory);
        }
예제 #6
0
        private async Task <BackedUpDirectory> AddDirectory(string directoryName, int?parentId = null)
        {
            var dir = new BackedUpDirectory()
            {
                Name     = directoryName,
                ParentId = parentId
            };

            dir = await GetFromPost($"{_serverUri}/{WebApi.AddDirectory}",
                                    JsonConvert.SerializeObject(dir), typeof(BackedUpDirectory))
                  .ConfigureAwait(false) as BackedUpDirectory;

            return(dir);
        }
예제 #7
0
        public IActionResult Update([FromBody] BackedUpDirectory directory)
        {
            if (directory == null)
            {
                return(BadRequest());
            }

            try
            {
                _dbService.UpdateDirectory(directory);
                return(Json(directory));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
        }
예제 #8
0
        public void AddDirAndDir()
        {
            var dir1 = new BackedUpDirectory()
            {
                Modified = DateTime.Now,
                Name     = @"c:\root2"
            };

            _dbService.AddDirectory(dir1);

            var dir2 = new BackedUpDirectory()
            {
                Modified = DateTime.Now,
                Name     = @"sub2",
                ParentId = dir1.Id
            };

            _dbService.AddDirectory(dir2);
        }