btcpayserver/BTCPayServer/Storage/Services/StoredFileRepository.cs
Andrew Camilleri b184360eb7 Abstracted cloud storage - Amazon/Google/Azure/Local (#708)
* wip

* add in storage system

* ui fixes

* fix settings ui

* Add Files Crud UI

* add titles

* link files to users

* add migration

* set blob to public

* remove base 64 read code

* fix file query model init

* move view model to own file

* fix local root path

* use datadir for local storage

* move to services

* add direct file url

* try fix tests

* remove magic string

* remove other magic strings

* show error message on unsupported provider

* fix asp net version

* redirect to storage settings if provider is not supported

* start writing tests

* fix tests

* fix test again

* add some more to the tests

* more tests

* try making local provider work on tests

* fix formfile

* fix small issue with returning deleted file

* check if returned data is null for deleted file

* validate azure Container name

* more state fixes

* change azure test trait

* add tmp file url generator

* fix tests

* small clean

* disable amazon and  google


comment out unused code for now


comment out google/amazon
2019-04-22 16:41:20 +09:00

69 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BTCPayServer.Data;
using BTCPayServer.Storage.Models;
using Microsoft.EntityFrameworkCore;
namespace BTCPayServer.Storage.Services
{
public class StoredFileRepository
{
private readonly ApplicationDbContextFactory _ApplicationDbContextFactory;
public StoredFileRepository(ApplicationDbContextFactory applicationDbContextFactory)
{
_ApplicationDbContextFactory = applicationDbContextFactory;
}
public async Task<StoredFile> GetFile(string fileId)
{
var filesResult = await GetFiles(new FilesQuery() {Id = new string[] {fileId}});
return filesResult.FirstOrDefault();
}
public async Task<List<StoredFile>> GetFiles(FilesQuery filesQuery = null)
{
if (filesQuery == null)
{
filesQuery = new FilesQuery();
}
using (var context = _ApplicationDbContextFactory.CreateContext())
{
return await context.Files
.Include(file => file.ApplicationUser)
.Where(file =>
(!filesQuery.Id.Any() || filesQuery.Id.Contains(file.Id)) &&
(!filesQuery.UserIds.Any() || filesQuery.UserIds.Contains(file.ApplicationUserId)))
.ToListAsync();
}
}
public async Task RemoveFile(StoredFile file)
{
using (var context = _ApplicationDbContextFactory.CreateContext())
{
context.Attach(file);
context.Files.Remove(file);
await context.SaveChangesAsync();
}
}
public async Task AddFile(StoredFile storedFile)
{
using (var context = _ApplicationDbContextFactory.CreateContext())
{
await context.AddAsync(storedFile);
await context.SaveChangesAsync();
}
}
public class FilesQuery
{
public string[] Id { get; set; } = Array.Empty<string>();
public string[] UserIds { get; set; } = Array.Empty<string>();
}
}
}