mirror of
https://github.com/ppy/osu.git
synced 2025-01-26 23:13:20 +08:00
Merge pull request #22361 from Susko3/delete-on-import-android
Delete files when importing on Android
This commit is contained in:
commit
2ba90b5447
63
osu.Android/AndroidImportTask.cs
Normal file
63
osu.Android/AndroidImportTask.cs
Normal file
@ -0,0 +1,63 @@
|
||||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. 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<AndroidImportTask?> 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;
|
||||
|
||||
if (!cursor.MoveToFirst())
|
||||
return null;
|
||||
|
||||
int filenameColumn = cursor.GetColumnIndex(IOpenableColumns.DisplayName);
|
||||
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.
|
||||
// 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))
|
||||
{
|
||||
if (stream == null)
|
||||
return null;
|
||||
|
||||
await stream.CopyToAsync(copy).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return new AndroidImportTask(copy, filename, contentResolver, uri);
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
||||
|
@ -51,6 +51,15 @@ namespace osu.Game.Database
|
||||
: getReaderFrom(Path);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes the file that is encapsulated by this <see cref="ImportTask"/>.
|
||||
/// </summary>
|
||||
public virtual void DeleteFile()
|
||||
{
|
||||
if (File.Exists(Path))
|
||||
File.Delete(Path);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an <see cref="ArchiveReader"/> from a stream.
|
||||
/// </summary>
|
||||
|
@ -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)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user