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); }
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); }
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); }
private async Task DequeueTask() { try { while (true) { // _cancellationToken.ThrowIfCancellationRequested(); if (Paused) { await Task.Delay(100); } else if (_queue.TryDequeue(out var action)) { switch (action.Action) { case System.IO.WatcherChangeTypes.Created: var backedUpFile = new BackedUpFile() { Modified = action.Updated, Name = PathUtils.DirectoryName(action.Path) }; await _syncEngine.Upload(action.Path, backedUpFile).ConfigureAwait(false); break; default: Console.WriteLine($"{action.Path} {action.Action}"); break; } } else { await Task.Delay(100).ConfigureAwait(false); } } } catch (OperationCanceledException) { Console.WriteLine("Cancellation requested. Shutting down action queue"); } catch (Exception ex) { Console.WriteLine($"Exception {ex.Message}"); } Console.WriteLine($"DequeueTask finishing"); }
public IActionResult Post([FromBody] BackedUpFile file) { if (file == null) { return(BadRequest()); } try { _dbService.UpdateFile(file); return(Json(file)); } catch (Exception ex) { return(BadRequest(ex)); } }
public async Task <string> Upload(string filePath, BackedUpFile backedUpFile) { using (var client = new HttpClient() { Timeout = httpClientTimeout }) using (var formData = new MultipartFormDataContent()) using (var fileStream = File.OpenRead(filePath)) { formData.Add(new StringContent(JsonConvert.SerializeObject(backedUpFile)), "backedUpFile"); formData.Add(new StringContent(filePath), "path"); formData.Add(new StreamContent(fileStream), "file", backedUpFile.Name); using (var response = await client.PostAsync($"{_serverUri}/{WebApi.UploadFile}", formData)) { var input = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { return(null); } return(await response.Content.ReadAsStringAsync()); } } }
// [ValidateAntiForgeryToken] public async Task <IActionResult> Upload() { if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType)) { return(BadRequest($"Expected a multipart request, but got {Request.ContentType}")); } // Used to accumulate all the form url encoded key value pairs in the // request. var formAccumulator = new KeyValueAccumulator(); string tempFilePath = null; var boundary = MultipartRequestHelper.GetBoundary( MediaTypeHeaderValue.Parse(Request.ContentType), _apiOptions.MultipartBoundaryLengthLimit); var reader = new MultipartReader(boundary, HttpContext.Request.Body); var section = await reader.ReadNextSectionAsync(); while (section != null) { var hasContentDispositionHeader = ContentDispositionHeaderValue .TryParse(section.ContentDisposition, out ContentDispositionHeaderValue contentDisposition); if (hasContentDispositionHeader) { if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition)) { tempFilePath = Path.GetTempFileName(); using (var targetStream = System.IO.File.Create(tempFilePath)) { await section.Body.CopyToAsync(targetStream); } } else if (MultipartRequestHelper.HasFormDataContentDisposition(contentDisposition)) { var key = HeaderUtilities.RemoveQuotes(contentDisposition.Name); var encoding = GetEncoding(section); using (var streamReader = new StreamReader( section.Body, encoding, detectEncodingFromByteOrderMarks: true, bufferSize: 1024, leaveOpen: true)) { // The value length limit is enforced by MultipartBodyLengthLimit var value = await streamReader.ReadToEndAsync(); if (String.Equals(value, "undefined", StringComparison.OrdinalIgnoreCase)) { value = String.Empty; } formAccumulator.Append(key, value); if (formAccumulator.ValueCount > _apiOptions.ValueCountLimit) { throw new InvalidDataException($"Form key count limit {_apiOptions.ValueCountLimit} exceeded."); } } } } // Drains any remaining section body that has not been consumed and // reads the headers for the next section. section = await reader.ReadNextSectionAsync(); } // Bind form data to a model var formData = formAccumulator.GetResults(); BackedUpFile backedUpFile = null; if (formData.TryGetValue("backedUpFile", out var values)) { var jsonString = values[0]; backedUpFile = JsonConvert.DeserializeObject <BackedUpFile>(jsonString); if (backedUpFile.ParentId == 0) { if (formData.TryGetValue("path", out var path)) { path = Path.GetDirectoryName(path) .Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); var dir = _dbService.GetDirectory(path); backedUpFile.ParentId = dir.Id; } } } if (backedUpFile.ParentId == 0) { throw new Exception($"{backedUpFile.Name} has no parent"); } var formValueProvider = new FormValueProvider( BindingSource.Form, new FormCollection(formAccumulator.GetResults()), CultureInfo.CurrentCulture); var bindingSuccessful = await TryUpdateModelAsync(backedUpFile, prefix : "", valueProvider : formValueProvider); if (!bindingSuccessful) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } } else { try { FileHistory hist = null; if (backedUpFile.Id > 0) { hist = _dbService.UpdateFile(backedUpFile); } else { hist = _dbService.AddFile(backedUpFile); } if (!Directory.Exists(_storageOptions.BackupRoot)) { Directory.CreateDirectory(_storageOptions.BackupRoot); } var destPath = Path.Combine(_storageOptions.BackupRoot, $"{hist.Id}_{backedUpFile.Name}"); if (System.IO.File.Exists(destPath)) { throw new IOException($"file '{destPath}' exists when it shouldn't"); } System.IO.File.Move(tempFilePath, destPath); } catch (Exception ex) { return(BadRequest(ex)); } } // filePath is where the file is return(Json(backedUpFile)); }