Skip to content

Commit 7699cf8

Browse files
ZeabytePikachuEXE
authored and
Alban Dumas
committed
Added functionality to export a single playlist (FreeTubeApp#5779)
* Added functionality to export a single playlist * Changed whitespace replacement in export file names to underscore Co-authored-by: PikachuEXE <[email protected]> * Added regular expression to replace common forbidden characters in export file name * Optimized regex for replacing illegal characters in file names * Prefer replaceAll over replace * * Make button hidden for online playlist, local playlist w/o video, move it before "remove duplicate" button --------- Co-authored-by: PikachuEXE <[email protected]>
1 parent efca575 commit 7699cf8

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

src/renderer/components/playlist-info/playlist-info.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import {
1010
ctrlFHandler,
1111
formatNumber,
1212
showToast,
13+
getTodayDateStrLocalTimezone,
14+
writeFileFromDialog,
15+
showSaveDialog,
1316
} from '../../helpers/utils'
1417
import debounce from 'lodash.debounce'
1518

@@ -262,6 +265,13 @@ export default defineComponent({
262265
return this.selectedUserPlaylist.videos.length - this.userPlaylistUniqueVideoIds.size
263266
},
264267

268+
exportPlaylistButtonVisible: function() {
269+
// Only online playlists can be shared
270+
if (!this.isUserPlaylist) { return false }
271+
272+
return this.videoCount > 0
273+
},
274+
265275
deletePlaylistButtonVisible: function() {
266276
if (!this.isUserPlaylist) { return false }
267277
// Cannot delete during edit
@@ -275,7 +285,6 @@ export default defineComponent({
275285
// Only online playlists can be shared
276286
if (this.isUserPlaylist) { return false }
277287

278-
// Cannot delete protected playlist
279288
return !this.hideSharingActions
280289
},
281290

@@ -412,6 +421,41 @@ export default defineComponent({
412421
showToast(this.playlistDeletionDisabledLabel)
413422
},
414423

424+
handlePlaylistExport: async function () {
425+
const dateStr = getTodayDateStrLocalTimezone()
426+
const title = this.selectedUserPlaylist.playlistName.replaceAll(' ', '_').replaceAll(/["%*/:<>?\\|]/g, '_')
427+
const exportFileName = 'freetube-playlist-' + title + '-' + dateStr + '.db'
428+
429+
const options = {
430+
defaultPath: exportFileName,
431+
filters: [
432+
{
433+
name: 'Database File',
434+
extensions: ['db']
435+
}
436+
]
437+
}
438+
439+
const data = JSON.stringify(this.selectedUserPlaylist) + '\n'
440+
441+
// See data-settings.js `promptAndWriteToFile`
442+
const response = await showSaveDialog(options)
443+
if (response.canceled || response.filePath === '') {
444+
// User canceled the save dialog
445+
return
446+
}
447+
448+
try {
449+
await writeFileFromDialog(response, data)
450+
} catch (writeErr) {
451+
const message = this.$t('Settings.Data Settings.Unable to write file')
452+
showToast(`${message}: ${writeErr}`)
453+
return
454+
}
455+
456+
showToast(this.$t('User Playlists.The playlist has been successfully exported'))
457+
},
458+
415459
exitEditMode: function () {
416460
this.editMode = false
417461

src/renderer/components/playlist-info/playlist-info.vue

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@
169169
theme="secondary"
170170
@click="toggleCopyVideosPrompt"
171171
/>
172+
<ft-icon-button
173+
v-if="exportPlaylistButtonVisible"
174+
:title="$t('User Playlists.Export Playlist')"
175+
:icon="['fas', 'file-arrow-down']"
176+
theme="secondary"
177+
@click="handlePlaylistExport"
178+
/>
172179
<ft-icon-button
173180
v-if="!editMode && userPlaylistDuplicateItemCount > 0"
174181
:title="$t('User Playlists.Remove Duplicate Videos')"

static/locales/en-US.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ User Playlists:
189189
Remove Watched Videos: Remove Watched Videos
190190
Enable Quick Bookmark With This Playlist: Enable Quick Bookmark With This Playlist
191191
Quick Bookmark Enabled: Quick Bookmark Enabled
192+
Export Playlist: Export This Playlist
193+
The playlist has been successfully exported: The playlist has been successfully exported
192194
Are you sure you want to remove {playlistItemCount} duplicate videos from this playlist? This cannot be undone: Are you sure you want to remove 1 duplicate video from this playlist? This cannot be undone. | Are you sure you want to remove {playlistItemCount} duplicate videos from this playlist? This cannot be undone.
193195
Are you sure you want to remove {playlistItemCount} watched videos from this playlist? This cannot be undone: Are you sure you want to remove 1 watched video from this playlist? This cannot be undone. | Are you sure you want to remove {playlistItemCount} watched videos from this playlist? This cannot be undone.
194196
Delete Playlist: Delete Playlist

0 commit comments

Comments
 (0)