Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const IpcChannels = {

GENERATE_PO_TOKEN: 'generate-po-token',

GET_SCREENSHOT_FALLBACK_FOLDER: 'get-screenshot-fallback-folder',
CHOOSE_DEFAULT_FOLDER: 'choose-default-folder',
WRITE_TO_DEFAULT_FOLDER: 'write-to-default-folder',

Expand Down
63 changes: 43 additions & 20 deletions src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from '../constants'
import * as baseHandlers from '../datastores/handlers/base'
import { extractExpiryTimestamp, ImageCache } from './ImageCache'
import { existsSync } from 'fs'
import { constants as fsConstants, existsSync } from 'fs'
import asyncFs from 'fs/promises'
import { promisify } from 'util'
import { brotliDecompress } from 'zlib'
Expand Down Expand Up @@ -1310,21 +1310,11 @@ function runApp() {
}
})

ipcMain.handle(IpcChannels.GET_SCREENSHOT_FALLBACK_FOLDER, (event) => {
if (isFreeTubeUrl(event.senderFrame.url)) {
return path.join(app.getPath('pictures'), 'Freetube')
}
})

ipcMain.on(IpcChannels.CHOOSE_DEFAULT_FOLDER, async (event) => {
if (!isFreeTubeUrl(event.senderFrame.url)) {
return
}

const settingId = 'screenshotFolderPath'

let currentPath = (await baseHandlers.settings._findOne(settingId))?.value

/**
* @param {import('electron').WebContents} webContents
* @param {string | undefined} [currentPath]
*/
async function chooseDefaultFolder(webContents, currentPath) {
if (typeof currentPath !== 'string' || currentPath.length === 0) {
currentPath = app.getPath('pictures')
}
Expand All @@ -1336,7 +1326,7 @@ function runApp() {

let result

const window = BrowserWindow.fromWebContents(event.sender)
const window = BrowserWindow.fromWebContents(webContents)
if (window) {
result = await dialog.showOpenDialog(window, dialogOptions)
} else {
Expand All @@ -1347,6 +1337,8 @@ function runApp() {
return
}

const settingId = 'screenshotFolderPath'

await baseHandlers.settings.upsert(settingId, result.filePaths[0])

const syncPayload = {
Expand All @@ -1362,6 +1354,22 @@ function runApp() {
window.webContents.send(IpcChannels.SYNC_SETTINGS, syncPayload)
}
})

return result.filePaths[0]
}

ipcMain.on(IpcChannels.CHOOSE_DEFAULT_FOLDER, async (event) => {
if (!isFreeTubeUrl(event.senderFrame.url)) {
return
}

let currentPath = (await baseHandlers.settings._findOne('screenshotFolderPath'))?.value

await chooseDefaultFolder(event.sender, currentPath)

if (typeof currentPath !== 'string' || currentPath.length === 0) {
currentPath = app.getPath('pictures')
}
})

ipcMain.handle(IpcChannels.WRITE_TO_DEFAULT_FOLDER, async (event, filename, arrayBuffer) => {
Expand All @@ -1376,9 +1384,22 @@ function runApp() {

let directory
if (typeof folderPath === 'string' && folderPath.length > 0) {
directory = folderPath
} else {
directory = path.join(app.getPath('pictures'), 'FreeTube')
try {
await asyncFs.access(path.normalize(folderPath), fsConstants.W_OK)
directory = folderPath
} catch {}
}

// if setting is not set or we do not have write access to the folder
// prompt the user for a folder
// not having write access can happen if the user copies their settings to different machines
// or if they revoke a previously permitted folder in flatseal
if (directory === undefined) {
directory = await chooseDefaultFolder(event.sender)

if (typeof directory !== 'string' || directory.length === 0) {
return false
}
}

directory = path.normalize(directory)
Expand All @@ -1399,6 +1420,8 @@ function runApp() {
// throw a new error so that we don't expose the real error to the renderer
throw new Error('Failed to save')
}

return true
})

/** @type {Map<number, number>} */
Expand Down
10 changes: 2 additions & 8 deletions src/preload/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,10 @@ export default {
/**
* @param {string} filename
* @param {ArrayBuffer} contents
* @returns {Promise<boolean>}
*/
writeToDefaultFolder: async (filename, contents) => {
await ipcRenderer.invoke(IpcChannels.WRITE_TO_DEFAULT_FOLDER, filename, contents)
},

/**
* @returns {Promise<string>}
*/
getScreenshotFallbackFolder: () => {
return ipcRenderer.invoke(IpcChannels.GET_SCREENSHOT_FALLBACK_FOLDER)
return await ipcRenderer.invoke(IpcChannels.WRITE_TO_DEFAULT_FOLDER, filename, contents)
},

relaunch: () => {
Expand Down
24 changes: 2 additions & 22 deletions src/renderer/components/PlayerSettings/PlayerSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@
</p>
<FtInput
class="screenshotFolderPath"
:placeholder="screenshotFolderPlaceholder"
:placeholder="screenshotFolder"
:show-action-button="false"
:show-label="false"
:disabled="true"
Expand Down Expand Up @@ -255,7 +255,7 @@
</template>

<script setup>
import { computed, onMounted, ref, watch } from 'vue'
import { computed, onMounted, ref } from 'vue'
import { useI18n } from '../../composables/use-i18n-polyfill'

import FtSettingsSection from '../FtSettingsSection/FtSettingsSection.vue'
Expand Down Expand Up @@ -607,40 +607,20 @@ function updateScreenshotAskPath(value) {
store.dispatch('updateScreenshotAskPath', value)
}

const screenshotFolderPlaceholder = ref('')

/** @type {import('vue').ComputedRef<string>} */
const screenshotFolder = computed(() => store.getters.getScreenshotFolderPath)

watch(screenshotFolder, () => {
getScreenshotFolderPlaceholder()
})

function chooseScreenshotFolder() {
// only use with electron
if (process.env.IS_ELECTRON) {
window.ftElectron.chooseDefaultFolder()
}
}

async function getScreenshotFolderPlaceholder() {
if (screenshotFolder.value !== '') {
screenshotFolderPlaceholder.value = screenshotFolder.value
return
}

if (process.env.IS_ELECTRON) {
screenshotFolderPlaceholder.value = await window.ftElectron.getScreenshotFallbackFolder()
} else {
screenshotFolderPlaceholder.value = ''
}
}

/** @type {import('vue').ComputedRef<string>} */
const screenshotFilenamePattern = computed(() => store.getters.getScreenshotFilenamePattern)

onMounted(() => {
getScreenshotFolderPlaceholder()
getScreenshotFilenameExample(screenshotFilenamePattern.value)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1733,9 +1733,9 @@ export default defineComponent({
} else {
const arrayBuffer = await blob.arrayBuffer()

await window.ftElectron.writeToDefaultFolder(filenameWithExtension, arrayBuffer)

showToast(t('Screenshot Success'))
if (await window.ftElectron.writeToDefaultFolder(filenameWithExtension, arrayBuffer)) {
showToast(t('Screenshot Success'))
}
}
} catch (error) {
console.error(error)
Expand Down