From f8537c1cbe98bc38f0423e8eabdc2332a21d22ab Mon Sep 17 00:00:00 2001 From: Susko3 Date: Sun, 22 Jan 2023 22:19:04 +0100 Subject: [PATCH 1/3] Delegate file deletion to `ImportTask` to allow overriding it --- osu.Game/Database/ImportTask.cs | 9 +++++++++ osu.Game/Database/RealmArchiveModelImporter.cs | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/osu.Game/Database/ImportTask.cs b/osu.Game/Database/ImportTask.cs index e7f599d85f..def20bc1fb 100644 --- a/osu.Game/Database/ImportTask.cs +++ b/osu.Game/Database/ImportTask.cs @@ -51,6 +51,15 @@ namespace osu.Game.Database : getReaderFrom(Path); } + /// + /// Deletes the file that is encapsulated by this . + /// + public virtual void DeleteFile() + { + if (File.Exists(Path)) + File.Delete(Path); + } + /// /// Creates an from a stream. /// diff --git a/osu.Game/Database/RealmArchiveModelImporter.cs b/osu.Game/Database/RealmArchiveModelImporter.cs index db8861c281..9d06c14b4b 100644 --- a/osu.Game/Database/RealmArchiveModelImporter.cs +++ b/osu.Game/Database/RealmArchiveModelImporter.cs @@ -201,8 +201,8 @@ namespace osu.Game.Database // TODO: Add a check to prevent files from storage to be deleted. try { - if (import != null && File.Exists(task.Path) && ShouldDeleteArchive(task.Path)) - File.Delete(task.Path); + if (import != null && ShouldDeleteArchive(task.Path)) + task.DeleteFile(); } catch (Exception e) { From de21864bd133e1d63c534b4c9406d5948e7938da Mon Sep 17 00:00:00 2001 From: Susko3 Date: Sun, 22 Jan 2023 22:47:16 +0100 Subject: [PATCH 2/3] Move logic to `AndroidImportTask` and add delete mechanism --- osu.Android/AndroidImportTask.cs | 57 ++++++++++++++++++++++++++++++++ osu.Android/OsuGameActivity.cs | 28 ++++------------ 2 files changed, 63 insertions(+), 22 deletions(-) create mode 100644 osu.Android/AndroidImportTask.cs diff --git a/osu.Android/AndroidImportTask.cs b/osu.Android/AndroidImportTask.cs new file mode 100644 index 0000000000..ec946f71f3 --- /dev/null +++ b/osu.Android/AndroidImportTask.cs @@ -0,0 +1,57 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System.IO; +using System.Threading.Tasks; +using Android.Content; +using Android.Net; +using Android.Provider; +using osu.Game.Database; + +namespace osu.Android +{ + public class AndroidImportTask : ImportTask + { + private readonly ContentResolver contentResolver; + + private readonly Uri uri; + + private AndroidImportTask(Stream stream, string filename, ContentResolver contentResolver, Uri uri) + : base(stream, filename) + { + this.contentResolver = contentResolver; + this.uri = uri; + } + + public override void DeleteFile() + { + contentResolver.Delete(uri, null, null); + } + + public static async Task Create(ContentResolver contentResolver, Uri uri) + { + // there are more performant overloads of this method, but this one is the most backwards-compatible + // (dates back to API 1). + + var cursor = contentResolver.Query(uri, null, null, null, null); + + if (cursor == null) + return null; + + cursor.MoveToFirst(); + + int filenameColumn = cursor.GetColumnIndex(IOpenableColumns.DisplayName); + string filename = cursor.GetString(filenameColumn); + + // SharpCompress requires archive streams to be seekable, which the stream opened by + // OpenInputStream() seems to not necessarily be. + // copy to an arbitrary-access memory stream to be able to proceed with the import. + var copy = new MemoryStream(); + + using (var stream = contentResolver.OpenInputStream(uri)) + await stream.CopyToAsync(copy).ConfigureAwait(false); + + return new AndroidImportTask(copy, filename, contentResolver, uri); + } + } +} diff --git a/osu.Android/OsuGameActivity.cs b/osu.Android/OsuGameActivity.cs index ca3d628447..f0a6e4733c 100644 --- a/osu.Android/OsuGameActivity.cs +++ b/osu.Android/OsuGameActivity.cs @@ -5,7 +5,6 @@ using System; using System.Collections.Generic; -using System.IO; using System.Linq; using System.Reflection; using System.Threading.Tasks; @@ -14,7 +13,6 @@ using Android.Content; using Android.Content.PM; using Android.Graphics; using Android.OS; -using Android.Provider; using Android.Views; using osu.Framework.Android; using osu.Game.Database; @@ -131,28 +129,14 @@ namespace osu.Android await Task.WhenAll(uris.Select(async uri => { - // there are more performant overloads of this method, but this one is the most backwards-compatible - // (dates back to API 1). - var cursor = ContentResolver?.Query(uri, null, null, null, null); + var task = await AndroidImportTask.Create(ContentResolver!, uri).ConfigureAwait(false); - if (cursor == null) - return; - - cursor.MoveToFirst(); - - int filenameColumn = cursor.GetColumnIndex(IOpenableColumns.DisplayName); - string filename = cursor.GetString(filenameColumn); - - // SharpCompress requires archive streams to be seekable, which the stream opened by - // OpenInputStream() seems to not necessarily be. - // copy to an arbitrary-access memory stream to be able to proceed with the import. - var copy = new MemoryStream(); - using (var stream = ContentResolver.OpenInputStream(uri)) - await stream.CopyToAsync(copy).ConfigureAwait(false); - - lock (tasks) + if (task != null) { - tasks.Add(new ImportTask(copy, filename)); + lock (tasks) + { + tasks.Add(task); + } } })).ConfigureAwait(false); From e16105d0593411dbb84e9b0cc5949fbb8d9e1737 Mon Sep 17 00:00:00 2001 From: Susko3 Date: Sun, 22 Jan 2023 23:11:50 +0100 Subject: [PATCH 3/3] Fix NRT and add more safety --- osu.Android/AndroidImportTask.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/osu.Android/AndroidImportTask.cs b/osu.Android/AndroidImportTask.cs index ec946f71f3..7273a6da5c 100644 --- a/osu.Android/AndroidImportTask.cs +++ b/osu.Android/AndroidImportTask.cs @@ -38,10 +38,11 @@ namespace osu.Android if (cursor == null) return null; - cursor.MoveToFirst(); + if (!cursor.MoveToFirst()) + return null; int filenameColumn = cursor.GetColumnIndex(IOpenableColumns.DisplayName); - string filename = cursor.GetString(filenameColumn); + string filename = cursor.GetString(filenameColumn) ?? uri.Path ?? string.Empty; // SharpCompress requires archive streams to be seekable, which the stream opened by // OpenInputStream() seems to not necessarily be. @@ -49,7 +50,12 @@ namespace osu.Android var copy = new MemoryStream(); using (var stream = contentResolver.OpenInputStream(uri)) + { + if (stream == null) + return null; + await stream.CopyToAsync(copy).ConfigureAwait(false); + } return new AndroidImportTask(copy, filename, contentResolver, uri); }