public virtual IndexedFile ApplyModifications(IndexedFile p_objFile) { IndexedFile objReturn; JObject objJson; StreamWriter objfile; List <IndexedItem> objModified; try { objReturn = p_objFile; objJson = JObject.Parse(objReturn.Raw); objModified = objReturn.Items.FindAll((item) => { return(item.Modified); }); foreach (IndexedItem item in objModified) { ((JValue)objJson.SelectToken(item.Flat)).Value = item.Value; } objfile = new StreamWriter(objReturn.Path); objfile.Write(objJson.ToString()); objfile.Close(); objfile.Dispose(); objfile = null; objReturn.Raw = objJson.ToString(); return(objReturn); } catch (Exception ex) { throw ex; } }
protected virtual void Dispose(bool disposing) { lock (sync) { if (file != null) { file.Dispose(); file = null; } if (reader != null) { reader.Close(); reader = null; } if (writer != null) { writer.Close(); writer = null; } if (memory != null) { memory.Dispose(); memory = null; } } }
public IndexedFileStorage(Stream stream, int clusterSize, bool safeWrites) { this.file = new IndexedFile(stream, clusterSize, safeWrites); this.memory = new MemoryStream(); this.reader = new BinaryReader(memory); this.writer = new BinaryWriter(memory); }
public void HasNoFiles_RemoveFile_ShouldThrow() { // arrange var indexedFile = new IndexedFile(Hash.Parse("FF"), 1, DateTimeOffset.MinValue, null); // act/assert Assert.Throws <ArgumentException>(() => indexedFile.RemoveLocation("test.txt")); }
public override IndexedFile ApplyModifications(IndexedFile p_objFile) { IndexedFile objReturn; JObject objJson; Deserializer objDeserializer; Serializer objSerializer; StringWriter strRawJson; StringWriter strRawYaml; StreamWriter objfile; object objYaml; object objJsonRaw; Newtonsoft.Json.JsonSerializer objSerializerJson; List <IndexedItem> objModified; try { objReturn = p_objFile; objDeserializer = new Deserializer(); objYaml = objDeserializer.Deserialize(new StringReader(objReturn.Raw)); objSerializerJson = new Newtonsoft.Json.JsonSerializer(); strRawJson = new StringWriter(); objSerializerJson.Serialize(strRawJson, objYaml); objJson = JObject.Parse(strRawJson.ToString()); objModified = objReturn.Items.FindAll((item) => { return(item.Modified); }); foreach (IndexedItem item in objModified) { ((JValue)objJson.SelectToken(item.Flat)).Value = item.Value; } objJsonRaw = JsonHelper.Deserialize(objJson.ToString()); objSerializer = new Serializer(); strRawYaml = new StringWriter(); objSerializer.Serialize(strRawYaml, objJsonRaw); objfile = new StreamWriter(objReturn.Path); objfile.Write(strRawYaml.ToString()); objfile.Close(); objfile.Dispose(); objfile = null; objReturn.Raw = strRawYaml.ToString(); return(objReturn); } catch (Exception ex) { throw ex; } }
public override async Task <IndexedFile> ApplyModificationsAsync(IndexedFile p_objFile) { try { return(await Task.Run(() => { return ApplyModifications(p_objFile); })); } catch (Exception) { throw; } }
public FileInformation ToFileInformation(IndexedFile indexedFile, FileLocation fileLocation) { if (indexedFile.Files.All(x => x != fileLocation)) { throw new ArgumentException("Can only get file information if the location belongs to the indexed file.", nameof(fileLocation)); } return(new FileInformation(_fileSystem.Path.Combine(_rootDirectory, fileLocation.RelativeFilename).ToForwardSlashes(), fileLocation.CreatedOn, fileLocation.ModifiedOn, Hash.Parse(indexedFile.Hash), indexedFile.Length, indexedFile.FileCreatedOn, indexedFile.PhotoProperties, fileLocation.RelativeFilename)); }
public static FileInformation ToFileInformation(this IndexedFile file, string relativeFilename, IPhotoDirectory photoDirectory) { var fileLocation = file.GetFileByFilename(relativeFilename); if (fileLocation == null) { throw new ArgumentException($"The indexed file is not located at {relativeFilename}"); } return(photoDirectory.ToFileInformation(file, fileLocation)); }
public override IndexedFile ProcessFile(string p_strPath) { IndexedFile objReturn; object objYaml; Deserializer objDeserializer; Newtonsoft.Json.JsonSerializer objSerializer; StreamReader objFile; string strRawFile; StringWriter strRawJson; JObject objJson; try { if (!Extensions.Contains(Path.GetExtension(p_strPath))) { throw new Exception("Invalid file."); } objReturn = new IndexedFile(); objReturn.Name = Path.GetFileName(p_strPath); objReturn.Extension = Path.GetExtension(p_strPath); objReturn.Path = p_strPath; objFile = new StreamReader(p_strPath); strRawFile = objFile.ReadToEnd(); objFile.Close(); objFile.Dispose(); objReturn.Raw = strRawFile; objDeserializer = new Deserializer(); objYaml = objDeserializer.Deserialize(new StringReader(strRawFile)); objSerializer = new Newtonsoft.Json.JsonSerializer(); strRawJson = new StringWriter(); objSerializer.Serialize(strRawJson, objYaml); objJson = JObject.Parse(strRawJson.ToString()); objReturn.Items = GetItens(objJson, objReturn); return(objReturn); } catch (Exception ex) { throw ex; } }
private async Task <(IndexedFile, bool fromBb)> GetFileInformation(IFile file, IIndexedFileRepository repository) { var fileHash = ComputeHash(file); var indexedFile = await repository.FirstOrDefaultBySpecs(new FindByFileHashSpec(fileHash), new IncludeFileLocationsSpec()); if (indexedFile != null) { return(indexedFile, true); } var info = await _fileInformationLoader.Load(file); indexedFile = new IndexedFile(info.Hash, info.Length, info.FileCreatedOn, info.PhotoProperties); return(indexedFile, false); }
public virtual IndexedFile ProcessFile(string p_strPath) { IndexedFile objReturn; JObject objJson; StreamReader objFile; string strRawFile; try { if (!Extensions.Contains(Path.GetExtension(p_strPath))) { throw new Exception("Invalid file."); } objReturn = new IndexedFile(); objReturn.Name = Path.GetFileName(p_strPath); objReturn.Extension = Path.GetExtension(p_strPath); objReturn.Path = p_strPath; objFile = new StreamReader(p_strPath); strRawFile = objFile.ReadToEnd(); objFile.Close(); objFile.Dispose(); objReturn.Raw = strRawFile; objJson = JObject.Parse(strRawFile); objReturn.Items = GetItens(objJson, objReturn); return(objReturn); } catch (Exception) { throw; } }
public static FileWithProgress GetUserFileProgress(string userId, Guid fileId) { using (var db = new HonyomiContext()) { IndexedFile file = db.Files.Include(x => x.Book).ThenInclude(x => x.Files) .SingleOrDefault(x => x.IndexedFileId == fileId); if (file == null) { //bail when such a file does not exist return(null); } FileWithProgress result = new FileWithProgress { Guid = fileId, Title = file.Title, BookGuid = file.BookId, BookTitle = file.Book.Title, TrackIndex = file.TrackIndex, MediaType = file.MimeType, Duration = file.Duration, NextFile = file.Book.Files.FirstOrDefault(x => x.TrackIndex == file.TrackIndex + 1)?.IndexedFileId ?? Guid.Empty }; FileProgress fProg = db.FileProgresses.SingleOrDefault(x => x.FileId == fileId && x.UserId == userId); if (fProg == null) { result.ProgressSeconds = 0; } else { result.ProgressSeconds = fProg.Progress; } return(result); } }
protected List <IndexedItem> GetItens(JToken p_objJson, IndexedFile p_objParent) { List <IndexedItem> objReturn; try { objReturn = new List <IndexedItem>(); foreach (JToken item in p_objJson.Children()) { if (item.Type == JTokenType.Property || item.Type == JTokenType.Object) { objReturn.AddRange(GetItens(item, p_objParent)); } else { if (item is JArray) { foreach (JToken subItem in item) { if (subItem is JValue) { objReturn.Add(new IndexedItem()); objReturn.Last().ParentFile = p_objParent; objReturn.Last().Flat = item.Path; objReturn.Last().Index = item.ToList().IndexOf(subItem); objReturn.Last().Alias = ((JProperty)item.Parent).Name; objReturn.Last().Tiemstamp = DateTime.Now; objReturn.Last().Modified = false; if (((JValue)subItem).Value != null) { objReturn.Last().Value = ((JValue)subItem).Value.ToString(); } else { objReturn.Last().Value = ""; } } else { objReturn.AddRange(GetItens(subItem, p_objParent)); } } } else { objReturn.Add(new IndexedItem()); objReturn.Last().ParentFile = p_objParent; objReturn.Last().Flat = item.Path; objReturn.Last().Alias = ((JProperty)item.Parent).Name; objReturn.Last().Tiemstamp = DateTime.Now; objReturn.Last().Modified = false; if (((JValue)item).Value != null) { objReturn.Last().Value = ((JValue)item).Value.ToString(); } else { objReturn.Last().Value = ""; } } } } return(objReturn); } catch (Exception ex) { throw ex; } }
public static IEnumerable <IFileInfo> ToFileInfos(this IndexedFile file, IPhotoDirectory photoDirectory) { return(file.Files.Select(x => photoDirectory.ToFileInformation(file, x))); }
public static FileInformation ToFileInformation(this IndexedFile file, IPhotoDirectory photoDirectory) { return(photoDirectory.ToFileInformation(file, file.Files.First())); }
public void Deconstruct(out IndexedFile indexedFile, out FileLocation fileLocation) { indexedFile = IndexedFile; fileLocation = FileLocation; }
public AddFileToIndexResponse(IndexedFile indexedFile, FileLocation fileLocation, bool isNewFile) { IndexedFile = indexedFile; FileLocation = fileLocation; IsNewFile = isNewFile; }