Skip to content

Commit 6a5012d

Browse files
authored
Fixed moving from PC to MTP (phone) storage (#8044)
1 parent 529db14 commit 6a5012d

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/Files/Filesystem/StorageItems/SystemStorageFile.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,48 @@ public override IAsyncAction CopyAndReplaceAsync(IStorageFile fileToReplace)
7777

7878
public override IAsyncAction MoveAsync(IStorageFolder destinationFolder)
7979
{
80-
return File.MoveAsync(destinationFolder);
80+
return MoveAsync(destinationFolder, Name, NameCollisionOption.FailIfExists);
8181
}
8282

8383
public override IAsyncAction MoveAsync(IStorageFolder destinationFolder, string desiredNewName)
8484
{
85-
return File.MoveAsync(destinationFolder, desiredNewName);
85+
return MoveAsync(destinationFolder, desiredNewName, NameCollisionOption.FailIfExists);
8686
}
8787

8888
public override IAsyncAction MoveAsync(IStorageFolder destinationFolder, string desiredNewName, NameCollisionOption option)
8989
{
90-
return File.MoveAsync(destinationFolder, desiredNewName, option);
90+
return AsyncInfo.Run(async (cancellationToken) =>
91+
{
92+
var destFolder = destinationFolder.AsBaseStorageFolder(); // Avoid calling IStorageFolder method
93+
if (destFolder is SystemStorageFolder sysFolder)
94+
{
95+
// File created by CreateFileAsync will get immediately deleted on MTP?! (#7206)
96+
await File.MoveAsync(sysFolder.Folder, desiredNewName, option);
97+
return;
98+
}
99+
var destFile = await destFolder.CreateFileAsync(desiredNewName, option.Convert());
100+
using (var inStream = await this.OpenStreamForReadAsync())
101+
using (var outStream = await destFile.OpenStreamForWriteAsync())
102+
{
103+
await inStream.CopyToAsync(outStream);
104+
await outStream.FlushAsync();
105+
}
106+
// Move unsupported, copy but do not delete original
107+
});
91108
}
92109

93110
public override IAsyncAction MoveAndReplaceAsync(IStorageFile fileToReplace)
94111
{
95-
return File.MoveAndReplaceAsync(fileToReplace);
112+
return AsyncInfo.Run(async (cancellationToken) =>
113+
{
114+
using (var inStream = await this.OpenStreamForReadAsync())
115+
using (var outStream = await fileToReplace.OpenStreamForWriteAsync())
116+
{
117+
await inStream.CopyToAsync(outStream);
118+
await outStream.FlushAsync();
119+
}
120+
// Move unsupported, copy but do not delete original
121+
});
96122
}
97123

98124
public override string ContentType => File.ContentType;

src/Files/ViewModels/ItemViewModel.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,8 +1726,6 @@ private async Task<CloudDriveSyncStatus> CheckCloudDriveSyncStatusAsync(IStorage
17261726
return (CloudDriveSyncStatus)syncStatus;
17271727
}
17281728

1729-
private StorageItemQueryResult itemQueryResult;
1730-
17311729
private async void WatchForStorageFolderChanges(BaseStorageFolder rootFolder)
17321730
{
17331731
if (rootFolder == null)
@@ -1745,7 +1743,7 @@ await Task.Run(() =>
17451743
options.SetThumbnailPrefetch(ThumbnailMode.ListView, 0, ThumbnailOptions.ReturnOnlyIfCached);
17461744
if (rootFolder.AreQueryOptionsSupported(options))
17471745
{
1748-
itemQueryResult = rootFolder.CreateItemQueryWithOptions(options).ToStorageItemQueryResult();
1746+
var itemQueryResult = rootFolder.CreateItemQueryWithOptions(options).ToStorageItemQueryResult();
17491747
itemQueryResult.ContentsChanged += ItemQueryResult_ContentsChanged;
17501748
var watchedItemsOperation = itemQueryResult.GetItemsAsync(0, 1); // Just get one item to start getting notifications
17511749
watcherCTS.Token.Register(() =>

0 commit comments

Comments
 (0)