file garbage collection

This commit is contained in:
2019-03-23 11:29:50 +01:00
parent b041c5fde1
commit 8842a192c7
7 changed files with 63 additions and 6 deletions

View File

@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<!--<add key="url" value="http://localhost/API"/>-->
<add key="url" value="http://test.benjamin-ifland.de"/>
<add key="url" value="http://localhost/API"/>
<!--<add key="url" value="http://test.benjamin-ifland.de"/>-->
</appSettings>
</configuration>

View File

@@ -154,6 +154,7 @@
</Compile>
<Compile Include="Migrations\Configuration.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\FileGarbageCollectionService.cs" />
<Compile Include="Services\FileService.cs" />
<Compile Include="Services\Logger.cs" />
</ItemGroup>

View File

@@ -33,6 +33,7 @@ namespace API.App_Start {
builder.RegisterType<Logger>();
builder.RegisterType<FileService>().As<IFileService>().SingleInstance();
builder.RegisterType<FileGarbageCollectionService>().As<IFileGarbageCollectionService>();
}
private static void RegisterDataProvider(this ContainerBuilder builder) {

View File

@@ -12,15 +12,17 @@ namespace API.Controllers {
public class FilesController : ApiController {
private readonly DataContext dataContext;
private readonly IFileService fileService;
private readonly IFileGarbageCollectionService fileGarbageCollectionService;
private readonly string dataPath;
public FilesController(
DataContext dataContext,
IFileService fileService
IFileService fileService,
IFileGarbageCollectionService fileGarbageCollectionService
) {
this.dataContext = dataContext;
this.fileService = fileService;
this.fileGarbageCollectionService = fileGarbageCollectionService;
dataPath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/App_Data");
}
@@ -28,6 +30,7 @@ namespace API.Controllers {
[Route("api/songs/{songId}/files/{fileId}")]
public HttpResponseMessage Get(long songId, long fileId) {
var response = new HttpResponseMessage(HttpStatusCode.OK);
fileGarbageCollectionService.Collect(dataPath);
var file = dataContext.Files.Find(fileId);
if (file == null || file.Song.ID != songId) return new HttpResponseMessage(HttpStatusCode.NotFound);
@@ -46,6 +49,8 @@ namespace API.Controllers {
// POST: api/Files
[Route("api/songs/{songId}/files")]
public async Task<HttpResponseMessage> Post(long songId, CancellationToken c) {
fileGarbageCollectionService.Collect(dataPath);
// get file from network input
var files = HttpContext.Current.Request.Files;
if (files.Count != 1) return new HttpResponseMessage(HttpStatusCode.BadRequest);

View File

@@ -0,0 +1,34 @@
using API.Database;
using System.Linq;
namespace API.Services {
public interface IFileGarbageCollectionService {
void Collect(string path);
}
public class FileGarbageCollectionService : IFileGarbageCollectionService {
private readonly IFileService fileService;
private readonly DataContext dataContext;
private readonly string[] fileExcludes;
public FileGarbageCollectionService(
IFileService fileService,
DataContext dataContext
) {
this.fileService = fileService;
this.dataContext = dataContext;
fileExcludes = new[] { "environment.json" };
}
public void Collect(string path) {
var filesInStoreage = fileService.ListFiles(path);
var filesInDatabase = dataContext.Files.Select(_ => _.ReferenceFile.ToString());
var filesWithoutReference = filesInStoreage
.Except(filesInDatabase)
.Except(fileExcludes);
foreach (var file in filesWithoutReference) {
fileService.Delete(path, file);
}
}
}
}

View File

@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
@@ -7,6 +9,8 @@ namespace API.Services {
public interface IFileService {
Task<Guid> Save(Stream file, string path, CancellationToken c);
Stream Load(string path, string filename);
void Delete(string path, string filename);
IEnumerable<string> ListFiles(string path);
}
public class FileService : IFileService {
@@ -21,12 +25,24 @@ namespace API.Services {
return filename;
}
public Stream Load(string path, string filename) {
var fullPath = Path.Combine(path, filename.ToString());
var fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.None, bufferSize, true);
return fileStream;
}
public void Delete(string path, string filename) {
var fullPath = Path.Combine(path, filename.ToString());
File.Delete(fullPath);
}
public IEnumerable<string> ListFiles(string path) {
var directoryInfo = new DirectoryInfo(path);
var files = directoryInfo.GetFiles();
var fileNames = files.Select(_ => _.Name);
return fileNames;
}
}
}

View File

@@ -19,7 +19,7 @@
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.6.2"/>
<httpRuntime targetFramework="4.6.2"/>
<httpRuntime targetFramework="4.6.2" maxRequestLength="30000000"/>
</system.web>
<system.webServer>
<modules>