diff --git a/osu.Game/Beatmaps/BeatmapManager.cs b/osu.Game/Beatmaps/BeatmapManager.cs
index e9f41f6bff..b48ab6112e 100644
--- a/osu.Game/Beatmaps/BeatmapManager.cs
+++ b/osu.Game/Beatmaps/BeatmapManager.cs
@@ -260,7 +260,7 @@ namespace osu.Game.Beatmaps
fileInfo.Filename = beatmapInfo.Path;
stream.Seek(0, SeekOrigin.Begin);
- UpdateFile(setInfo, fileInfo, stream);
+ ReplaceFile(setInfo, fileInfo, stream);
}
}
diff --git a/osu.Game/Database/ArchiveModelManager.cs b/osu.Game/Database/ArchiveModelManager.cs
index 76bc4f7755..c39b71b058 100644
--- a/osu.Game/Database/ArchiveModelManager.cs
+++ b/osu.Game/Database/ArchiveModelManager.cs
@@ -401,29 +401,55 @@ namespace osu.Game.Database
}
///
- /// Update an existing file, or create a new entry if not already part of the 's files.
+ /// Replace an existing file with a new version.
///
/// The item to operate on.
- /// The file model to be updated or added.
+ /// The existing file to be replaced.
/// The new file contents.
- public void UpdateFile(TModel model, TFileModel file, Stream contents)
+ /// An optional filename for the new file. Will use the previous filename if not specified.
+ public void ReplaceFile(TModel model, TFileModel file, Stream contents, string filename = null)
+ {
+ using (ContextFactory.GetForWrite())
+ {
+ DeleteFile(model, file);
+ AddFile(model, contents, filename ?? file.Filename);
+ }
+ }
+
+ ///
+ /// Delete new file.
+ ///
+ /// The item to operate on.
+ /// The existing file to be deleted.
+ public void DeleteFile(TModel model, TFileModel file)
{
using (var usage = ContextFactory.GetForWrite())
{
// Dereference the existing file info, since the file model will be removed.
if (file.FileInfo != null)
- {
Files.Dereference(file.FileInfo);
- // Remove the file model.
- usage.Context.Set().Remove(file);
- }
+ // This shouldn't be required, but here for safety in case the provided TModel is not being change tracked
+ // Definitely can be removed once we rework the database backend.
+ usage.Context.Set().Remove(file);
- // Add the new file info and containing file model.
model.Files.Remove(file);
+ }
+ }
+
+ ///
+ /// Add a new file.
+ ///
+ /// The item to operate on.
+ /// The new file contents.
+ /// The filename for the new file.
+ public void AddFile(TModel model, Stream contents, string filename)
+ {
+ using (ContextFactory.GetForWrite())
+ {
model.Files.Add(new TFileModel
{
- Filename = file.Filename,
+ Filename = filename,
FileInfo = Files.Add(contents)
});