public bool DeleteFile(MysteryFile file) { var record = GetRecord(file.guid); if (record == null) { return(false); } //I have one, but is it the last copy? var different_copy = (from x in GetRecords(record.hash) where x.guid != record.guid select x).FirstOrDefault(); _collection.DeleteOne(x => x.guid == record.guid); if (different_copy == null) { //last copy File.Delete(record.path); } else { //we don't we still have at least 1 reference out there. } //no matter if we still have a copy for somebody else we say we delete this one return(true); }
public Stream GetFileStream(MysteryFile file) { var record = GetRecord(file.guid); if (record == null) { return(null); } var result = new FileStream(record.path, FileMode.Open, FileAccess.Read, FileShare.Read); return(result); }
public byte[] GetFileBytes(MysteryFile file) { var record = GetRecord(file.guid); if (record == null) { return(null); } using (var file_stream = new FileStream(record.path, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (var mem = new MemoryStream()) { file_stream.CopyTo(mem); return(mem.ToArray()); } } }
public MysteryFile CloneInTmpFile(MysteryFile file, DateTime expiration) { var record = GetRecord(file.guid); if (record == null) { return(null); } var clone = record.Clone(); clone.guid = Guid.NewGuid(); clone.expiration = expiration; _collection.InsertOne(clone); return(new MysteryFile() { filename = file.filename, guid = clone.guid }); }