예제 #1
0
        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);
        }
예제 #2
0
        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);
        }
예제 #3
0
        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());
                }
            }
        }
예제 #4
0
        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
            });
        }