mirror of
https://github.com/osukey/osukey.git
synced 2025-05-02 04:07:22 +09:00
117 lines
3.5 KiB
C#
117 lines
3.5 KiB
C#
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using osu.Framework.Extensions;
|
|
using osu.Framework.IO.Stores;
|
|
using osu.Framework.Logging;
|
|
using osu.Framework.Platform;
|
|
using osu.Game.Database;
|
|
|
|
namespace osu.Game.IO
|
|
{
|
|
/// <summary>
|
|
/// Handles the Store and retrieval of Files/FileSets to the database backing
|
|
/// </summary>
|
|
public class FileStore : DatabaseBackedStore
|
|
{
|
|
private const string prefix = "files";
|
|
|
|
public readonly ResourceStore<byte[]> Store;
|
|
|
|
public FileStore(Func<OsuDbContext> contextSource, Storage storage) : base(contextSource, storage)
|
|
{
|
|
Store = new NamespacedResourceStore<byte[]>(new StorageBackedResourceStore(storage), prefix);
|
|
}
|
|
|
|
protected override void Prepare(bool reset = false)
|
|
{
|
|
if (reset)
|
|
{
|
|
if (Storage.ExistsDirectory(prefix))
|
|
Storage.DeleteDirectory(prefix);
|
|
|
|
Context.Database.ExecuteSqlCommand("DELETE FROM FileInfo");
|
|
}
|
|
}
|
|
|
|
protected override void StartupTasks()
|
|
{
|
|
base.StartupTasks();
|
|
deletePending();
|
|
}
|
|
|
|
public FileInfo Add(Stream data, bool reference = true)
|
|
{
|
|
string hash = data.ComputeSHA2Hash();
|
|
|
|
var existing = Context.FileInfo.FirstOrDefault(f => f.Hash == hash);
|
|
|
|
var info = existing ?? new FileInfo { Hash = hash };
|
|
|
|
string path = Path.Combine(prefix, info.StoragePath);
|
|
|
|
// we may be re-adding a file to fix missing store entries.
|
|
if (!Storage.Exists(path))
|
|
{
|
|
data.Seek(0, SeekOrigin.Begin);
|
|
|
|
using (var output = Storage.GetStream(path, FileAccess.Write))
|
|
data.CopyTo(output);
|
|
|
|
data.Seek(0, SeekOrigin.Begin);
|
|
}
|
|
|
|
if (reference || existing == null)
|
|
Reference(info);
|
|
|
|
return info;
|
|
}
|
|
|
|
public void Reference(params FileInfo[] files)
|
|
{
|
|
foreach (var f in files.GroupBy(f => f.ID))
|
|
{
|
|
var refetch = Context.Find<FileInfo>(f.First().ID) ?? f.First();
|
|
refetch.ReferenceCount += f.Count();
|
|
Context.FileInfo.Update(refetch);
|
|
}
|
|
|
|
Context.SaveChanges();
|
|
}
|
|
|
|
public void Dereference(params FileInfo[] files)
|
|
{
|
|
foreach (var f in files.GroupBy(f => f.ID))
|
|
{
|
|
var refetch = Context.Find<FileInfo>(f.First().ID);
|
|
refetch.ReferenceCount -= f.Count();
|
|
Context.Update(refetch);
|
|
}
|
|
|
|
Context.SaveChanges();
|
|
}
|
|
|
|
private void deletePending()
|
|
{
|
|
foreach (var f in Context.FileInfo.Where(f => f.ReferenceCount < 1))
|
|
{
|
|
try
|
|
{
|
|
Storage.Delete(Path.Combine(prefix, f.StoragePath));
|
|
Context.FileInfo.Remove(f);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Logger.Error(e, $@"Could not delete beatmap {f}");
|
|
}
|
|
}
|
|
|
|
Context.SaveChanges();
|
|
}
|
|
}
|
|
}
|