From c2ddac724d820ac5d881cde187768c0277ced71f Mon Sep 17 00:00:00 2001 From: bob1520 <80553357+hockerschwan@users.noreply.github.com> Date: Tue, 21 Jun 2022 01:43:45 +0000 Subject: [PATCH] Fix download filename problems (#2320) * fix "illegal" filename and EISDIR * check download folder exists --- src/renderer/store/modules/utils.js | 49 ++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/src/renderer/store/modules/utils.js b/src/renderer/store/modules/utils.js index d0d2be5e..911385c8 100644 --- a/src/renderer/store/modules/utils.js +++ b/src/renderer/store/modules/utils.js @@ -1,6 +1,7 @@ import IsEqual from 'lodash.isequal' import FtToastEvents from '../../components/ft-toast/ft-toast-events' import fs from 'fs' +import path from 'path' import i18n from '../../i18n/index' import { IpcChannels } from '../../../constants' @@ -180,8 +181,41 @@ const actions = { } }, + replaceFilenameForbiddenChars(_, filenameOriginal) { + let filenameNew = filenameOriginal + let forbiddenChars = {} + switch (process.platform) { + case 'win32': + forbiddenChars = { + '<': '<', // U+FF1C + '>': '>', // U+FF1E + ':': ':', // U+FF1A + '"': '"', // U+FF02 + '/': '/', // U+FF0F + '\\': '\', // U+FF3C + '|': '|', // U+FF5C + '?': '?', // U+FF1F + '*': '*' // U+FF0A + } + break + case 'darwin': + forbiddenChars = { '/': '/', ':': ':' } + break + case 'linux': + forbiddenChars = { '/': '/' } + break + default: + break + } + + for (const forbiddenChar in forbiddenChars) { + filenameNew = filenameNew.replaceAll(forbiddenChar, forbiddenChars[forbiddenChar]) + } + return filenameNew + }, + async downloadMedia({ rootState, dispatch }, { url, title, extension, fallingBackPath }) { - const fileName = `${title}.${extension}` + const fileName = `${await dispatch('replaceFilenameForbiddenChars', title)}.${extension}` const usingElectron = rootState.settings.usingElectron const locale = i18n._vm.locale const translations = i18n._vm.messages[locale] @@ -213,6 +247,19 @@ const actions = { } folderPath = response.filePath + } else { + if (!fs.existsSync(folderPath)) { + try { + fs.mkdirSync(folderPath, { recursive: true }) + } catch (err) { + console.error(err) + this.showToast({ + message: err + }) + return + } + } + folderPath = path.join(folderPath, fileName) } dispatch('showToast', {