2017-07-26 11:22:02 +00:00
|
|
|
|
// 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 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>
|
2017-07-27 07:56:41 +00:00
|
|
|
|
public class FileStore : DatabaseBackedStore
|
2017-07-26 11:22:02 +00:00
|
|
|
|
{
|
2017-10-17 08:08:19 +00:00
|
|
|
|
public readonly IResourceStore<byte[]> Store;
|
2017-07-26 11:22:02 +00:00
|
|
|
|
|
2017-10-30 13:24:11 +00:00
|
|
|
|
public new Storage Storage => base.Storage;
|
2017-07-26 11:22:02 +00:00
|
|
|
|
|
2017-10-23 07:35:35 +00:00
|
|
|
|
public FileStore(Func<OsuDbContext> createContext, Storage storage) : base(createContext, storage.GetStorageForDirectory(@"files"))
|
2017-07-26 11:22:02 +00:00
|
|
|
|
{
|
2017-10-17 08:08:19 +00:00
|
|
|
|
Store = new StorageBackedResourceStore(Storage);
|
2017-07-26 11:22:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-19 09:34:58 +00:00
|
|
|
|
public FileInfo Add(Stream data, bool reference = true)
|
2017-07-26 11:22:02 +00:00
|
|
|
|
{
|
2017-10-17 06:50:42 +00:00
|
|
|
|
var context = GetContext();
|
|
|
|
|
|
2017-07-27 08:40:25 +00:00
|
|
|
|
string hash = data.ComputeSHA2Hash();
|
2017-07-26 11:22:02 +00:00
|
|
|
|
|
2017-10-17 06:50:42 +00:00
|
|
|
|
var existing = context.FileInfo.FirstOrDefault(f => f.Hash == hash);
|
2017-07-26 11:22:02 +00:00
|
|
|
|
|
2017-08-01 08:37:21 +00:00
|
|
|
|
var info = existing ?? new FileInfo { Hash = hash };
|
2017-07-26 11:22:02 +00:00
|
|
|
|
|
2017-10-17 08:08:19 +00:00
|
|
|
|
string path = info.StoragePath;
|
2017-09-19 09:34:58 +00:00
|
|
|
|
|
|
|
|
|
// we may be re-adding a file to fix missing store entries.
|
|
|
|
|
if (!Storage.Exists(path))
|
|
|
|
|
{
|
2017-07-26 11:22:02 +00:00
|
|
|
|
data.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
2017-09-19 09:34:58 +00:00
|
|
|
|
using (var output = Storage.GetStream(path, FileAccess.Write))
|
|
|
|
|
data.CopyTo(output);
|
2017-07-26 11:22:02 +00:00
|
|
|
|
|
|
|
|
|
data.Seek(0, SeekOrigin.Begin);
|
2017-09-19 09:34:58 +00:00
|
|
|
|
}
|
2017-07-26 11:22:02 +00:00
|
|
|
|
|
2017-09-19 09:34:58 +00:00
|
|
|
|
if (reference || existing == null)
|
|
|
|
|
Reference(info);
|
|
|
|
|
|
2017-07-26 11:22:02 +00:00
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-17 06:50:42 +00:00
|
|
|
|
public void Reference(params FileInfo[] files) => reference(GetContext(), files);
|
|
|
|
|
|
|
|
|
|
private void reference(OsuDbContext context, FileInfo[] files)
|
2017-07-26 11:22:02 +00:00
|
|
|
|
{
|
2017-10-16 04:09:10 +00:00
|
|
|
|
foreach (var f in files.GroupBy(f => f.ID))
|
2017-07-26 11:22:02 +00:00
|
|
|
|
{
|
2017-10-17 06:50:42 +00:00
|
|
|
|
var refetch = context.Find<FileInfo>(f.First().ID) ?? f.First();
|
2017-10-16 04:09:10 +00:00
|
|
|
|
refetch.ReferenceCount += f.Count();
|
2017-10-17 06:50:42 +00:00
|
|
|
|
context.FileInfo.Update(refetch);
|
2017-10-16 04:09:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-17 06:50:42 +00:00
|
|
|
|
context.SaveChanges();
|
2017-07-26 11:22:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-17 06:50:42 +00:00
|
|
|
|
public void Dereference(params FileInfo[] files) => dereference(GetContext(), files);
|
|
|
|
|
|
|
|
|
|
private void dereference(OsuDbContext context, FileInfo[] files)
|
2017-07-26 11:22:02 +00:00
|
|
|
|
{
|
2017-10-16 04:09:10 +00:00
|
|
|
|
foreach (var f in files.GroupBy(f => f.ID))
|
2017-07-26 11:22:02 +00:00
|
|
|
|
{
|
2017-10-18 09:27:17 +00:00
|
|
|
|
var refetch = context.FileInfo.Find(f.Key);
|
2017-10-17 01:25:18 +00:00
|
|
|
|
refetch.ReferenceCount -= f.Count();
|
2017-10-18 09:27:17 +00:00
|
|
|
|
context.FileInfo.Update(refetch);
|
2017-10-16 04:09:10 +00:00
|
|
|
|
}
|
2017-10-04 19:52:12 +00:00
|
|
|
|
|
2017-10-17 06:50:42 +00:00
|
|
|
|
context.SaveChanges();
|
2017-07-26 11:22:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-17 10:58:33 +00:00
|
|
|
|
public override void Cleanup()
|
2017-07-26 11:22:02 +00:00
|
|
|
|
{
|
2017-10-17 06:50:42 +00:00
|
|
|
|
var context = GetContext();
|
|
|
|
|
|
|
|
|
|
foreach (var f in context.FileInfo.Where(f => f.ReferenceCount < 1))
|
2017-07-26 11:22:02 +00:00
|
|
|
|
{
|
2017-10-04 19:52:12 +00:00
|
|
|
|
try
|
2017-07-26 11:22:02 +00:00
|
|
|
|
{
|
2017-10-17 08:08:19 +00:00
|
|
|
|
Storage.Delete(f.StoragePath);
|
2017-10-17 06:50:42 +00:00
|
|
|
|
context.FileInfo.Remove(f);
|
2017-07-26 11:22:02 +00:00
|
|
|
|
}
|
2017-10-04 19:52:12 +00:00
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Logger.Error(e, $@"Could not delete beatmap {f}");
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-17 01:25:18 +00:00
|
|
|
|
|
2017-10-17 06:50:42 +00:00
|
|
|
|
context.SaveChanges();
|
2017-07-26 11:22:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-04 19:52:12 +00:00
|
|
|
|
}
|