public void RemoveAttachment(BufferFileMeta meta) { var existingFile = _database.FileStorage.Find(GetStringIdFromFilename(meta, true)).FirstOrDefault(q => q.Filename == meta.Filename); if (existingFile == null) { return; } _database.FileStorage.Delete(existingFile.Id); if (meta.AttachmentType == BufferFileMeta.AttachmentTypes.Attachment) { var att = _database.GetCollection <FileAttachment>().FindOne(q => q.FileId == existingFile.Id); if (att != null) { _database.GetCollection <FileAttachment>().Delete(att.FileId); } } else if (meta.AttachmentType == BufferFileMeta.AttachmentTypes.DocLib) { var att = _database.GetCollection <FileDoclib>().FindOne(q => q.FileId == existingFile.Id); if (att != null) { _database.GetCollection <FileDoclib>().Delete(att.FileId); } } }
private string GetStringIdFromFilename(BufferFileMeta meta, bool pathOnly = false) { string stringId = $"{meta.AttachmentType}_{meta.Listname}_{meta.Folder}"; if (!pathOnly) { stringId += $"_{meta.ParentId}_{meta.Filename}"; } return(CleanUpLiteDbId(stringId)); }
public MemoryStream GetAttachmentStreamById(string id, out string filename, out BufferFileMeta meta) { var fileInfo = _database.FileStorage.FindById(id); filename = fileInfo.Filename; MemoryStream fileStream = new MemoryStream((int)fileInfo.Length); fileInfo.CopyTo(fileStream); meta = GetMetadataFromAttachment(fileInfo.Metadata); return(fileStream); }
public string AddAttachment(BufferFileMeta meta, Stream fileStream, FileBase.AllowedStates state) { int calculatedIndex = 0; string prefix = GetStringIdFromFilename(meta, true); var existingFiles = _database.FileStorage.Find(prefix); if (existingFiles.Count() > 0) { calculatedIndex = existingFiles.Min(q => int.Parse(q.Metadata["Id"].AsString ?? "0")); if (calculatedIndex > 0) { calculatedIndex = 0; } } calculatedIndex--; meta.SetId(calculatedIndex); string fileId = GetStringIdFromFilename(meta); //if (AttachmentExists(fileId)) _database.FileStorage.Delete(fileId); var uploadedFile = _database.FileStorage.Upload(fileId, meta.Filename, fileStream); _database.FileStorage.SetMetadata(uploadedFile.Id, GetMetadataFromAttachment(meta)); // string fileId = uploadedFile.Id; if (meta.AttachmentType == BufferFileMeta.AttachmentTypes.Attachment) { var collection = _database.GetCollection <FileAttachment>(); collection.Delete(fileId); // Delete old entry if exists collection.Insert(new FileAttachment { FileId = fileId, Filename = meta.Filename, List = meta.Listname, ReferenceId = meta.ParentId, State = state }); } else if (meta.AttachmentType == BufferFileMeta.AttachmentTypes.DocLib) { var collection = _database.GetCollection <FileDoclib>(); collection.Insert(new FileDoclib { FileId = fileId, Filename = meta.Filename, List = meta.Listname, ReferenceId = meta.ParentId, State = state, Folder = meta.Folder }); } return(fileId); }
public BsonDocument GetMetadataFromAttachment(BufferFileMeta meta) { var doc = new BsonDocument(); foreach (var property in typeof(BufferFileMeta).GetProperties()) { // doc.Set(property.Name, property.GetValue(meta)); if (property.CanRead) { doc[property.Name] = property.GetValue(meta)?.ToString(); } } return(doc); }
public BufferFileMeta GetMetadataFromAttachment(LiteDB.BsonDocument doc) { var meta = new BufferFileMeta(); foreach (var property in typeof(BufferFileMeta).GetProperties()) { if (property.CanWrite && doc.ContainsKey(property.Name)) { var converter = TypeDescriptor.GetConverter(property.PropertyType); property.SetValue(meta, converter.ConvertFromString(doc[property.Name])); } } meta.SetId(int.Parse(doc[nameof(BufferFileMeta.Id)].AsString)); return(meta); }
public void DeleteAttachmentFromDbWithoutSyncing(BufferFileMeta meta) { var db = new LiteDb(_helpers, _aweCsomeTable, _connectionString); db.RemoveAttachment(meta); }
public System.IO.MemoryStream GetAttachmentStreamFromDbById(string id, out string filename, out BufferFileMeta meta) { var db = new LiteDb(_helpers, _aweCsomeTable, _connectionString); return(db.GetAttachmentStreamById(id, out filename, out meta)); }