2018-02-15 15:33:33 +08:00
|
|
|
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
|
|
|
|
using System;
|
2018-02-14 19:26:49 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2018-02-15 11:21:11 +08:00
|
|
|
|
using System.Linq;
|
2018-02-14 19:26:49 +08:00
|
|
|
|
using Ionic.Zip;
|
2018-02-15 11:21:11 +08:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2018-02-14 19:26:49 +08:00
|
|
|
|
using osu.Framework.Logging;
|
|
|
|
|
using osu.Framework.Platform;
|
|
|
|
|
using osu.Game.IO;
|
2018-02-15 11:56:22 +08:00
|
|
|
|
using osu.Game.IO.Archives;
|
2018-02-14 19:26:49 +08:00
|
|
|
|
using osu.Game.IPC;
|
|
|
|
|
using osu.Game.Overlays.Notifications;
|
|
|
|
|
using FileInfo = osu.Game.IO.FileInfo;
|
|
|
|
|
|
2018-02-15 11:21:11 +08:00
|
|
|
|
namespace osu.Game.Database
|
2018-02-14 19:26:49 +08:00
|
|
|
|
{
|
2018-02-15 11:56:22 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Encapsulates a model store class to give it import functionality.
|
|
|
|
|
/// Adds cross-functionality with <see cref="FileStore"/> to give access to the central file store for the provided model.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TModel">The model type.</typeparam>
|
|
|
|
|
/// <typeparam name="TFileModel">The associated file join type.</typeparam>
|
2018-02-15 13:19:16 +08:00
|
|
|
|
public abstract class ArchiveModelManager<TModel, TFileModel> : ICanAcceptFiles
|
2018-02-15 11:21:11 +08:00
|
|
|
|
where TModel : class, IHasFiles<TFileModel>, IHasPrimaryKey, ISoftDelete
|
2018-02-14 19:26:49 +08:00
|
|
|
|
where TFileModel : INamedFileInfo, new()
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Set an endpoint for notifications to be posted to.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Action<Notification> PostNotification { protected get; set; }
|
|
|
|
|
|
2018-02-15 11:56:22 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fired when a new <see cref="TModel"/> becomes available in the database.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event Action<TModel> ItemAdded;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fired when a <see cref="TModel"/> is removed from the database.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event Action<TModel> ItemRemoved;
|
|
|
|
|
|
2018-02-14 19:26:49 +08:00
|
|
|
|
public virtual string[] HandledExtensions => new[] { ".zip" };
|
|
|
|
|
|
|
|
|
|
protected readonly FileStore Files;
|
|
|
|
|
|
|
|
|
|
protected readonly IDatabaseContextFactory ContextFactory;
|
|
|
|
|
|
2018-02-15 11:56:22 +08:00
|
|
|
|
protected readonly MutableDatabaseBackedStore<TModel> ModelStore;
|
2018-02-14 19:26:49 +08:00
|
|
|
|
|
|
|
|
|
// ReSharper disable once NotAccessedField.Local (we should keep a reference to this so it is not finalised)
|
|
|
|
|
private ArchiveImportIPCChannel ipc;
|
|
|
|
|
|
2018-02-15 11:56:22 +08:00
|
|
|
|
protected ArchiveModelManager(Storage storage, IDatabaseContextFactory contextFactory, MutableDatabaseBackedStore<TModel> modelStore, IIpcHost importHost = null)
|
2018-02-14 19:26:49 +08:00
|
|
|
|
{
|
|
|
|
|
ContextFactory = contextFactory;
|
2018-02-15 11:56:22 +08:00
|
|
|
|
|
2018-02-14 19:26:49 +08:00
|
|
|
|
ModelStore = modelStore;
|
2018-02-15 11:56:22 +08:00
|
|
|
|
ModelStore.ItemAdded += s => ItemAdded?.Invoke(s);
|
|
|
|
|
ModelStore.ItemRemoved += s => ItemRemoved?.Invoke(s);
|
|
|
|
|
|
2018-02-14 19:26:49 +08:00
|
|
|
|
Files = new FileStore(contextFactory, storage);
|
|
|
|
|
|
|
|
|
|
if (importHost != null)
|
|
|
|
|
ipc = new ArchiveImportIPCChannel(importHost, this);
|
2018-02-15 12:30:17 +08:00
|
|
|
|
|
2018-02-15 15:31:42 +08:00
|
|
|
|
ModelStore.Cleanup();
|
2018-02-14 19:26:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2018-02-15 11:56:22 +08:00
|
|
|
|
/// Import one or more <see cref="TModel"/> items from filesystem <paramref name="paths"/>.
|
2018-02-14 19:26:49 +08:00
|
|
|
|
/// This will post notifications tracking progress.
|
|
|
|
|
/// </summary>
|
2018-02-15 11:56:22 +08:00
|
|
|
|
/// <param name="paths">One or more archive locations on disk.</param>
|
2018-02-14 19:26:49 +08:00
|
|
|
|
public void Import(params string[] paths)
|
|
|
|
|
{
|
|
|
|
|
var notification = new ProgressNotification
|
|
|
|
|
{
|
|
|
|
|
Text = "Import is initialising...",
|
|
|
|
|
Progress = 0,
|
|
|
|
|
State = ProgressNotificationState.Active,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
PostNotification?.Invoke(notification);
|
|
|
|
|
|
|
|
|
|
List<TModel> imported = new List<TModel>();
|
|
|
|
|
|
2018-03-19 19:30:45 +08:00
|
|
|
|
int current = 0;
|
2018-03-19 19:15:17 +08:00
|
|
|
|
int errors = 0;
|
2018-02-14 19:26:49 +08:00
|
|
|
|
foreach (string path in paths)
|
|
|
|
|
{
|
|
|
|
|
if (notification.State == ProgressNotificationState.Cancelled)
|
|
|
|
|
// user requested abort
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-03-19 19:30:45 +08:00
|
|
|
|
notification.Text = $"Importing ({++current} of {paths.Length})\n{Path.GetFileName(path)}";
|
2018-02-14 19:26:49 +08:00
|
|
|
|
using (ArchiveReader reader = getReaderFrom(path))
|
|
|
|
|
imported.Add(Import(reader));
|
|
|
|
|
|
2018-03-19 19:30:45 +08:00
|
|
|
|
notification.Progress = (float)(current - 1) / paths.Length;
|
2018-02-14 19:26:49 +08:00
|
|
|
|
|
|
|
|
|
// We may or may not want to delete the file depending on where it is stored.
|
2018-02-15 11:56:22 +08:00
|
|
|
|
// e.g. reconstructing/repairing database with items from default storage.
|
2018-02-14 19:26:49 +08:00
|
|
|
|
// Also, not always a single file, i.e. for LegacyFilesystemReader
|
|
|
|
|
// TODO: Add a check to prevent files from storage to be deleted.
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(path))
|
|
|
|
|
File.Delete(path);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Logger.Error(e, $@"Could not delete original file after import ({Path.GetFileName(path)})");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
e = e.InnerException ?? e;
|
2018-02-15 11:56:22 +08:00
|
|
|
|
Logger.Error(e, $@"Could not import ({Path.GetFileName(path)})");
|
2018-03-19 19:15:17 +08:00
|
|
|
|
errors++;
|
2018-02-14 19:26:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-19 19:30:45 +08:00
|
|
|
|
notification.Text = errors > 0 ? $"Import complete with {errors} errors" : "Import successful!";
|
|
|
|
|
notification.State = ProgressNotificationState.Completed;
|
2018-02-14 19:26:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2018-02-15 11:56:22 +08:00
|
|
|
|
/// Import an item from an <see cref="ArchiveReader"/>.
|
2018-02-14 19:26:49 +08:00
|
|
|
|
/// </summary>
|
2018-02-15 11:56:22 +08:00
|
|
|
|
/// <param name="archive">The archive to be imported.</param>
|
2018-02-14 19:26:49 +08:00
|
|
|
|
public TModel Import(ArchiveReader archive)
|
|
|
|
|
{
|
|
|
|
|
using (ContextFactory.GetForWrite()) // used to share a context for full import. keep in mind this will block all writes.
|
|
|
|
|
{
|
2018-02-15 11:56:22 +08:00
|
|
|
|
// create a new model (don't yet add to database)
|
|
|
|
|
var item = CreateModel(archive);
|
2018-02-14 19:26:49 +08:00
|
|
|
|
|
2018-02-15 11:56:22 +08:00
|
|
|
|
var existing = CheckForExisting(item);
|
2018-02-14 19:26:49 +08:00
|
|
|
|
|
|
|
|
|
if (existing != null) return existing;
|
|
|
|
|
|
2018-02-15 11:56:22 +08:00
|
|
|
|
item.Files = createFileInfos(archive, Files);
|
2018-02-14 19:26:49 +08:00
|
|
|
|
|
2018-02-15 11:56:22 +08:00
|
|
|
|
Populate(item, archive);
|
2018-02-14 19:26:49 +08:00
|
|
|
|
|
|
|
|
|
// import to store
|
2018-02-15 11:56:22 +08:00
|
|
|
|
ModelStore.Add(item);
|
2018-02-14 19:26:49 +08:00
|
|
|
|
|
2018-02-15 11:56:22 +08:00
|
|
|
|
return item;
|
2018-02-14 19:26:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-15 11:21:11 +08:00
|
|
|
|
/// <summary>
|
2018-02-15 11:56:22 +08:00
|
|
|
|
/// Import an item from a <see cref="TModel"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The model to be imported.</param>
|
|
|
|
|
public void Import(TModel item) => ModelStore.Add(item);
|
|
|
|
|
|
2018-02-15 12:30:17 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Perform an update of the specified item.
|
|
|
|
|
/// TODO: Support file changes.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item to update.</param>
|
|
|
|
|
public void Update(TModel item) => ModelStore.Update(item);
|
|
|
|
|
|
2018-02-15 11:56:22 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Delete an item from the manager.
|
|
|
|
|
/// Is a no-op for already deleted items.
|
2018-02-15 11:21:11 +08:00
|
|
|
|
/// </summary>
|
2018-02-15 11:56:22 +08:00
|
|
|
|
/// <param name="item">The item to delete.</param>
|
|
|
|
|
public void Delete(TModel item)
|
2018-02-15 11:21:11 +08:00
|
|
|
|
{
|
|
|
|
|
using (var usage = ContextFactory.GetForWrite())
|
|
|
|
|
{
|
|
|
|
|
var context = usage.Context;
|
|
|
|
|
|
|
|
|
|
context.ChangeTracker.AutoDetectChangesEnabled = false;
|
|
|
|
|
|
|
|
|
|
// re-fetch the model on the import context.
|
2018-02-15 11:56:22 +08:00
|
|
|
|
var foundModel = queryModel().Include(s => s.Files).ThenInclude(f => f.FileInfo).First(s => s.ID == item.ID);
|
2018-02-15 11:21:11 +08:00
|
|
|
|
|
2018-02-15 11:56:22 +08:00
|
|
|
|
if (foundModel.DeletePending) return;
|
2018-02-15 11:21:11 +08:00
|
|
|
|
|
|
|
|
|
if (ModelStore.Delete(foundModel))
|
|
|
|
|
Files.Dereference(foundModel.Files.Select(f => f.FileInfo).ToArray());
|
|
|
|
|
|
|
|
|
|
context.ChangeTracker.AutoDetectChangesEnabled = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-15 11:56:22 +08:00
|
|
|
|
/// <summary>
|
2018-02-15 12:30:17 +08:00
|
|
|
|
/// Delete multiple items.
|
2018-02-15 11:56:22 +08:00
|
|
|
|
/// This will post notifications tracking progress.
|
|
|
|
|
/// </summary>
|
2018-02-15 12:30:17 +08:00
|
|
|
|
public void Delete(List<TModel> items)
|
2018-02-15 11:56:22 +08:00
|
|
|
|
{
|
2018-02-15 12:30:17 +08:00
|
|
|
|
if (items.Count == 0) return;
|
|
|
|
|
|
|
|
|
|
var notification = new ProgressNotification
|
|
|
|
|
{
|
|
|
|
|
Progress = 0,
|
|
|
|
|
CompletionText = "Deleted all beatmaps!",
|
|
|
|
|
State = ProgressNotificationState.Active,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
PostNotification?.Invoke(notification);
|
|
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
|
|
using (ContextFactory.GetForWrite())
|
|
|
|
|
{
|
|
|
|
|
foreach (var b in items)
|
|
|
|
|
{
|
|
|
|
|
if (notification.State == ProgressNotificationState.Cancelled)
|
|
|
|
|
// user requested abort
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
notification.Text = $"Deleting ({i} of {items.Count})";
|
|
|
|
|
notification.Progress = (float)++i / items.Count;
|
|
|
|
|
Delete(b);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-15 11:56:22 +08:00
|
|
|
|
|
2018-02-15 12:30:17 +08:00
|
|
|
|
notification.State = ProgressNotificationState.Completed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Restore multiple items that were previously deleted.
|
|
|
|
|
/// This will post notifications tracking progress.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Undelete(List<TModel> items)
|
|
|
|
|
{
|
|
|
|
|
if (!items.Any()) return;
|
2018-02-15 11:56:22 +08:00
|
|
|
|
|
|
|
|
|
var notification = new ProgressNotification
|
|
|
|
|
{
|
|
|
|
|
CompletionText = "Restored all deleted items!",
|
|
|
|
|
Progress = 0,
|
|
|
|
|
State = ProgressNotificationState.Active,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
PostNotification?.Invoke(notification);
|
|
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
2018-02-15 12:30:17 +08:00
|
|
|
|
using (ContextFactory.GetForWrite())
|
2018-02-15 11:56:22 +08:00
|
|
|
|
{
|
2018-02-15 12:30:17 +08:00
|
|
|
|
foreach (var item in items)
|
|
|
|
|
{
|
|
|
|
|
if (notification.State == ProgressNotificationState.Cancelled)
|
|
|
|
|
// user requested abort
|
|
|
|
|
return;
|
2018-02-15 11:56:22 +08:00
|
|
|
|
|
2018-02-15 12:30:17 +08:00
|
|
|
|
notification.Text = $"Restoring ({i} of {items.Count})";
|
|
|
|
|
notification.Progress = (float)++i / items.Count;
|
|
|
|
|
Undelete(item);
|
|
|
|
|
}
|
2018-02-15 11:56:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notification.State = ProgressNotificationState.Completed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Restore an item that was previously deleted. Is a no-op if the item is not in a deleted state, or has its protected flag set.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item to restore</param>
|
|
|
|
|
public void Undelete(TModel item)
|
|
|
|
|
{
|
|
|
|
|
using (var usage = ContextFactory.GetForWrite())
|
|
|
|
|
{
|
|
|
|
|
usage.Context.ChangeTracker.AutoDetectChangesEnabled = false;
|
|
|
|
|
|
|
|
|
|
if (!ModelStore.Undelete(item)) return;
|
|
|
|
|
|
|
|
|
|
Files.Reference(item.Files.Select(f => f.FileInfo).ToArray());
|
|
|
|
|
|
|
|
|
|
usage.Context.ChangeTracker.AutoDetectChangesEnabled = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-14 19:26:49 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create all required <see cref="FileInfo"/>s for the provided archive, adding them to the global file store.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private List<TFileModel> createFileInfos(ArchiveReader reader, FileStore files)
|
|
|
|
|
{
|
|
|
|
|
var fileInfos = new List<TFileModel>();
|
|
|
|
|
|
|
|
|
|
// import files to manager
|
|
|
|
|
foreach (string file in reader.Filenames)
|
|
|
|
|
using (Stream s = reader.GetStream(file))
|
|
|
|
|
fileInfos.Add(new TFileModel
|
|
|
|
|
{
|
|
|
|
|
Filename = file,
|
|
|
|
|
FileInfo = files.Add(s)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return fileInfos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create a barebones model from the provided archive.
|
|
|
|
|
/// Actual expensive population should be done in <see cref="Populate"/>; this should just prepare for duplicate checking.
|
|
|
|
|
/// </summary>
|
2018-02-19 13:50:42 +08:00
|
|
|
|
/// <param name="archive">The archive to create the model for.</param>
|
|
|
|
|
/// <returns>A model populated with minimal information.</returns>
|
2018-02-14 19:26:49 +08:00
|
|
|
|
protected abstract TModel CreateModel(ArchiveReader archive);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Populate the provided model completely from the given archive.
|
|
|
|
|
/// After this method, the model should be in a state ready to commit to a store.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model">The model to populate.</param>
|
|
|
|
|
/// <param name="archive">The archive to use as a reference for population.</param>
|
|
|
|
|
protected virtual void Populate(TModel model, ArchiveReader archive)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-15 11:21:11 +08:00
|
|
|
|
protected virtual TModel CheckForExisting(TModel model) => null;
|
|
|
|
|
|
2018-02-15 11:56:22 +08:00
|
|
|
|
private DbSet<TModel> queryModel() => ContextFactory.Get().Set<TModel>();
|
2018-02-14 19:26:49 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates an <see cref="ArchiveReader"/> from a valid storage path.
|
|
|
|
|
/// </summary>
|
2018-02-15 11:21:11 +08:00
|
|
|
|
/// <param name="path">A file or folder path resolving the archive content.</param>
|
|
|
|
|
/// <returns>A reader giving access to the archive's content.</returns>
|
2018-02-14 19:26:49 +08:00
|
|
|
|
private ArchiveReader getReaderFrom(string path)
|
|
|
|
|
{
|
|
|
|
|
if (ZipFile.IsZipFile(path))
|
2018-02-15 11:56:22 +08:00
|
|
|
|
return new ZipArchiveReader(Files.Storage.GetStream(path), Path.GetFileName(path));
|
2018-03-20 17:01:09 +08:00
|
|
|
|
return new LegacyFilesystemReader(path);
|
2018-02-14 19:26:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|