1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-11-16 06:44:55 +00:00

Check if file really exists before asking user to overwrite it

This commit is contained in:
Stypox 2021-03-17 20:28:15 +01:00
parent b78ac7d2e9
commit 21b8df0375
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
3 changed files with 30 additions and 16 deletions

View File

@ -757,15 +757,14 @@ public class DownloadDialog extends DialogFragment
return; return;
} }
// check if is our file // get state of potential mission referring to the same file
final MissionState state = downloadManager.checkForExistingMission(storage); final MissionState state = downloadManager.checkForExistingMission(storage);
@StringRes @StringRes final int msgBtn;
final int msgBtn; @StringRes final int msgBody;
@StringRes
final int msgBody;
// this switch checks if there is already a mission referring to the same file
switch (state) { switch (state) {
case Finished: case Finished: // there is already a finished mission
msgBtn = R.string.overwrite; msgBtn = R.string.overwrite;
msgBody = R.string.overwrite_finished_warning; msgBody = R.string.overwrite_finished_warning;
break; break;
@ -777,7 +776,7 @@ public class DownloadDialog extends DialogFragment
msgBtn = R.string.generate_unique_name; msgBtn = R.string.generate_unique_name;
msgBody = R.string.download_already_running; msgBody = R.string.download_already_running;
break; break;
case None: case None: // there is no mission referring to the same file
if (mainStorage == null) { if (mainStorage == null) {
// This part is called if: // This part is called if:
// * using SAF on older android version // * using SAF on older android version
@ -812,7 +811,7 @@ public class DownloadDialog extends DialogFragment
msgBody = R.string.overwrite_unrelated_warning; msgBody = R.string.overwrite_unrelated_warning;
break; break;
default: default:
return; return; // unreachable
} }
final AlertDialog.Builder askDialog = new AlertDialog.Builder(context) final AlertDialog.Builder askDialog = new AlertDialog.Builder(context)

View File

@ -290,11 +290,10 @@ public class StoredFileHelper implements Serializable {
} }
// WARNING: DocumentFile.exists() and DocumentFile.isFile() methods are slow // WARNING: DocumentFile.exists() and DocumentFile.isFile() methods are slow
final boolean exists = docFile == null ? ioFile.exists() : docFile.exists(); // docFile.isVirtual() means it is non-physical?
// ¿docFile.isVirtual() means is no-physical? return docFile == null
final boolean isFile = docFile == null ? ioFile.isFile() : docFile.isFile(); ? (ioFile.exists() && ioFile.isFile())
: (docFile.exists() && docFile.isFile());
return exists && isFile;
} }
public boolean create() { public boolean create() {

View File

@ -106,7 +106,8 @@ public class DownloadManager {
} }
/** /**
* Loads finished missions from the data source * Loads finished missions from the data source and forgets finished missions whose file does
* not exist anymore.
*/ */
private ArrayList<FinishedMission> loadFinishedMissions() { private ArrayList<FinishedMission> loadFinishedMissions() {
ArrayList<FinishedMission> finishedMissions = mFinishedMissionStore.loadFinishedMissions(); ArrayList<FinishedMission> finishedMissions = mFinishedMissionStore.loadFinishedMissions();
@ -331,14 +332,29 @@ public class DownloadManager {
} }
/** /**
* Get a finished mission by its path * Get the index into {@link #mMissionsFinished} of a finished mission by its path, return
* {@code -1} if there is no such mission. This function also checks if the matched mission's
* file exists, and, if it does not, the related mission is forgotten about (like in {@link
* #loadFinishedMissions()}) and {@code -1} is returned.
* *
* @param storage where the file possible is stored * @param storage where the file would be stored
* @return the mission index or -1 if no such mission exists * @return the mission index or -1 if no such mission exists
*/ */
private int getFinishedMissionIndex(StoredFileHelper storage) { private int getFinishedMissionIndex(StoredFileHelper storage) {
for (int i = 0; i < mMissionsFinished.size(); i++) { for (int i = 0; i < mMissionsFinished.size(); i++) {
if (mMissionsFinished.get(i).storage.equals(storage)) { if (mMissionsFinished.get(i).storage.equals(storage)) {
// If the file does not exist the mission is not valid anymore. Also checking if
// length == 0 since the file picker may create an empty file before yielding it,
// but that does not mean the file really belonged to a previous mission.
if (!storage.existsAsFile() || storage.length() == 0) {
if (DEBUG) {
Log.d(TAG, "matched downloaded file removed: " + storage.getName());
}
mFinishedMissionStore.deleteMission(mMissionsFinished.get(i));
mMissionsFinished.remove(i);
return -1; // finished mission whose associated file was removed
}
return i; return i;
} }
} }