move: handle_same_name_exists to helpers.js

This commit is contained in:
KernelDeimos
2025-06-17 21:11:44 -04:00
parent a4b412dc46
commit 0eb436678b
2 changed files with 51 additions and 37 deletions
+1 -37
View File
@@ -1217,42 +1217,6 @@ const ipc_listener = async (event, handled) => {
$el_parent_disable_mask.css('z-index', parseInt($el_parent_window.css('z-index')) + 1);
$(target_iframe).blur();
const handle_same_name_exists = async ({
action, parent_uuid,
}) => {
try {
await action({ overwrite: false });
} catch ( err ) {
if ( err.code !== 'item_with_same_name_exists' ) {
await UIAlert({
message: err.message ?? "Upload failed.",
parent_uuid,
});
return false;
}
const alert_resp = await UIAlert({
message: `<strong>${html_encode(err.entry_name)}</strong> already exists.`,
buttons:[
{
label: i18n('replace'),
value: 'replace',
type: 'primary',
},
{
label: i18n('cancel'),
value: 'cancel',
},
],
parent_uuid,
});
if ( alert_resp === 'replace' ) {
await action({ overwrite: true });
return true;
}
return false;
}
}
const handle_url_save = async ({ target_path }) => {
// download progress tracker
let dl_op_id = window.operation_id++;
@@ -1348,7 +1312,7 @@ const ipc_listener = async (event, handled) => {
const handle_data_save = async ({ target_path, el_filedialog_window }) => {
let file_to_upload = new File([event.data.content], path.basename(target_path));
const written = await handle_same_name_exists({
const written = await window.handle_same_name_exists({
action: async ({ overwrite }) => {
console.log('action with overwrite flag:', overwrite);
await write_file_tell_caller_and_update_views({
+50
View File
@@ -2762,3 +2762,53 @@ window.format_credits = (num) => {
return window.format_with_units(num, { mulUnits })
};
/**
* This function will call the provided action function in a try...catch
* and handle the 'item_with_same_name_exists' error by re-calling the
* action with `{ overwrite: true }` if the user specifies they want to
* do so.
*
* All exceptions are trapped by this function. The user will see
* "Upload failed." if an error occurs and the error object will
* be logged to the console.
*
* A parent_uuid for a window should be specified for alert boxes to
* behave correctly.
*/
window.handle_same_name_exists = async ({
action, parent_uuid,
}) => {
try {
await action({ overwrite: false });
} catch ( err ) {
if ( err.code !== 'item_with_same_name_exists' ) {
console.error(err);
await UIAlert({
message: err.message ?? "Upload failed.",
parent_uuid,
});
return false;
}
const alert_resp = await UIAlert({
message: `<strong>${html_encode(err.entry_name)}</strong> already exists.`,
buttons:[
{
label: i18n('replace'),
value: 'replace',
type: 'primary',
},
{
label: i18n('cancel'),
value: 'cancel',
},
],
parent_uuid,
});
if ( alert_resp === 'replace' ) {
await action({ overwrite: true });
return true;
}
return false;
}
}