Exemple #1
0
        public async Task <FileReference> AddFile(Stream fileData, string relativeFilePath)
        {
            string         finalFilePath = Path.GetFullPath(Path.Combine(_rootPath, relativeFilePath));
            FileStoreEntry entry         = new FileStoreEntry(finalFilePath);

            lock (this)
            {
                if (_entries.ContainsKey(finalFilePath))
                {
                    throw new Exception("Files can't be added to the store more than once");
                }
                entry.FileUnreferenced += Entry_FileUnreferenced;
                _entries.Add(finalFilePath, entry);
            }
            Directory.CreateDirectory(Path.GetDirectoryName(finalFilePath));
            for (int i = 0; i < 10; i++)
            {
                try
                {
                    using (FileStream cachedFileStream = File.OpenWrite(finalFilePath))
                    {
                        await fileData.CopyToAsync(cachedFileStream);
                    }
                }
                catch (IOException)
                {
                    if (i == 9) // give up eventually
                    {
                        throw;
                    }
                }
            }

            return(entry.AddRef());
        }
Exemple #2
0
 public FileReference(FileStoreEntry entry)
 {
     _entry = entry;
 }
Exemple #3
0
 public void Dispose()
 {
     _entry.Release();
     _entry = null;
 }
Exemple #4
0
 private void RemoveFile(FileStoreEntry entry)
 {
     _entries.Remove(entry.FilePath);
     File.Delete(entry.FilePath);
 }