file garbage collection
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<configuration>
|
<configuration>
|
||||||
<appSettings>
|
<appSettings>
|
||||||
<!--<add key="url" value="http://localhost/API"/>-->
|
<add key="url" value="http://localhost/API"/>
|
||||||
<add key="url" value="http://test.benjamin-ifland.de"/>
|
<!--<add key="url" value="http://test.benjamin-ifland.de"/>-->
|
||||||
</appSettings>
|
</appSettings>
|
||||||
|
|
||||||
</configuration>
|
</configuration>
|
||||||
@@ -154,6 +154,7 @@
|
|||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="Migrations\Configuration.cs" />
|
<Compile Include="Migrations\Configuration.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
<Compile Include="Services\FileGarbageCollectionService.cs" />
|
||||||
<Compile Include="Services\FileService.cs" />
|
<Compile Include="Services\FileService.cs" />
|
||||||
<Compile Include="Services\Logger.cs" />
|
<Compile Include="Services\Logger.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ namespace API.App_Start {
|
|||||||
builder.RegisterType<Logger>();
|
builder.RegisterType<Logger>();
|
||||||
|
|
||||||
builder.RegisterType<FileService>().As<IFileService>().SingleInstance();
|
builder.RegisterType<FileService>().As<IFileService>().SingleInstance();
|
||||||
|
builder.RegisterType<FileGarbageCollectionService>().As<IFileGarbageCollectionService>();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void RegisterDataProvider(this ContainerBuilder builder) {
|
private static void RegisterDataProvider(this ContainerBuilder builder) {
|
||||||
|
|||||||
@@ -12,15 +12,17 @@ namespace API.Controllers {
|
|||||||
public class FilesController : ApiController {
|
public class FilesController : ApiController {
|
||||||
private readonly DataContext dataContext;
|
private readonly DataContext dataContext;
|
||||||
private readonly IFileService fileService;
|
private readonly IFileService fileService;
|
||||||
|
private readonly IFileGarbageCollectionService fileGarbageCollectionService;
|
||||||
private readonly string dataPath;
|
private readonly string dataPath;
|
||||||
|
|
||||||
public FilesController(
|
public FilesController(
|
||||||
DataContext dataContext,
|
DataContext dataContext,
|
||||||
IFileService fileService
|
IFileService fileService,
|
||||||
|
IFileGarbageCollectionService fileGarbageCollectionService
|
||||||
) {
|
) {
|
||||||
this.dataContext = dataContext;
|
this.dataContext = dataContext;
|
||||||
this.fileService = fileService;
|
this.fileService = fileService;
|
||||||
|
this.fileGarbageCollectionService = fileGarbageCollectionService;
|
||||||
dataPath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/App_Data");
|
dataPath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/App_Data");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,6 +30,7 @@ namespace API.Controllers {
|
|||||||
[Route("api/songs/{songId}/files/{fileId}")]
|
[Route("api/songs/{songId}/files/{fileId}")]
|
||||||
public HttpResponseMessage Get(long songId, long fileId) {
|
public HttpResponseMessage Get(long songId, long fileId) {
|
||||||
var response = new HttpResponseMessage(HttpStatusCode.OK);
|
var response = new HttpResponseMessage(HttpStatusCode.OK);
|
||||||
|
fileGarbageCollectionService.Collect(dataPath);
|
||||||
|
|
||||||
var file = dataContext.Files.Find(fileId);
|
var file = dataContext.Files.Find(fileId);
|
||||||
if (file == null || file.Song.ID != songId) return new HttpResponseMessage(HttpStatusCode.NotFound);
|
if (file == null || file.Song.ID != songId) return new HttpResponseMessage(HttpStatusCode.NotFound);
|
||||||
@@ -46,6 +49,8 @@ namespace API.Controllers {
|
|||||||
// POST: api/Files
|
// POST: api/Files
|
||||||
[Route("api/songs/{songId}/files")]
|
[Route("api/songs/{songId}/files")]
|
||||||
public async Task<HttpResponseMessage> Post(long songId, CancellationToken c) {
|
public async Task<HttpResponseMessage> Post(long songId, CancellationToken c) {
|
||||||
|
fileGarbageCollectionService.Collect(dataPath);
|
||||||
|
|
||||||
// get file from network input
|
// get file from network input
|
||||||
var files = HttpContext.Current.Request.Files;
|
var files = HttpContext.Current.Request.Files;
|
||||||
if (files.Count != 1) return new HttpResponseMessage(HttpStatusCode.BadRequest);
|
if (files.Count != 1) return new HttpResponseMessage(HttpStatusCode.BadRequest);
|
||||||
|
|||||||
34
API/API/Services/FileGarbageCollectionService.cs
Normal file
34
API/API/Services/FileGarbageCollectionService.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@@ -7,6 +9,8 @@ namespace API.Services {
|
|||||||
public interface IFileService {
|
public interface IFileService {
|
||||||
Task<Guid> Save(Stream file, string path, CancellationToken c);
|
Task<Guid> Save(Stream file, string path, CancellationToken c);
|
||||||
Stream Load(string path, string filename);
|
Stream Load(string path, string filename);
|
||||||
|
void Delete(string path, string filename);
|
||||||
|
IEnumerable<string> ListFiles(string path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class FileService : IFileService {
|
public class FileService : IFileService {
|
||||||
@@ -21,12 +25,24 @@ namespace API.Services {
|
|||||||
|
|
||||||
return filename;
|
return filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Stream Load(string path, string filename) {
|
public Stream Load(string path, string filename) {
|
||||||
var fullPath = Path.Combine(path, filename.ToString());
|
var fullPath = Path.Combine(path, filename.ToString());
|
||||||
var fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.None, bufferSize, true);
|
var fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.None, bufferSize, true);
|
||||||
return fileStream;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -19,7 +19,7 @@
|
|||||||
</appSettings>
|
</appSettings>
|
||||||
<system.web>
|
<system.web>
|
||||||
<compilation debug="true" targetFramework="4.6.2"/>
|
<compilation debug="true" targetFramework="4.6.2"/>
|
||||||
<httpRuntime targetFramework="4.6.2"/>
|
<httpRuntime targetFramework="4.6.2" maxRequestLength="30000000"/>
|
||||||
</system.web>
|
</system.web>
|
||||||
<system.webServer>
|
<system.webServer>
|
||||||
<modules>
|
<modules>
|
||||||
|
|||||||
Reference in New Issue
Block a user