Skip to content

Commit

Permalink
Added functionality to export a single playlist (#5779)
Browse files Browse the repository at this point in the history
* Added functionality to export a single playlist

* Changed whitespace replacement in export file names to underscore

Co-authored-by: PikachuEXE <git@pikachuexe.net>

* 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 <git@pikachuexe.net>
  • Loading branch information
Zeabyte and PikachuEXE authored Nov 4, 2024
1 parent 94fe62a commit d3b6563
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/renderer/components/playlist-info/playlist-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {
ctrlFHandler,
formatNumber,
showToast,
getTodayDateStrLocalTimezone,
writeFileFromDialog,
showSaveDialog,
} from '../../helpers/utils'
import debounce from 'lodash.debounce'

Expand Down Expand Up @@ -262,6 +265,13 @@ export default defineComponent({
return this.selectedUserPlaylist.videos.length - this.userPlaylistUniqueVideoIds.size
},

exportPlaylistButtonVisible: function() {
// Only online playlists can be shared
if (!this.isUserPlaylist) { return false }

return this.videoCount > 0
},

deletePlaylistButtonVisible: function() {
if (!this.isUserPlaylist) { return false }
// Cannot delete during edit
Expand All @@ -275,7 +285,6 @@ export default defineComponent({
// Only online playlists can be shared
if (this.isUserPlaylist) { return false }

// Cannot delete protected playlist
return !this.hideSharingActions
},

Expand Down Expand Up @@ -412,6 +421,41 @@ export default defineComponent({
showToast(this.playlistDeletionDisabledLabel)
},

handlePlaylistExport: async function () {
const dateStr = getTodayDateStrLocalTimezone()
const title = this.selectedUserPlaylist.playlistName.replaceAll(' ', '_').replaceAll(/["%*/:<>?\\|]/g, '_')
const exportFileName = 'freetube-playlist-' + title + '-' + dateStr + '.db'

const options = {
defaultPath: exportFileName,
filters: [
{
name: 'Database File',
extensions: ['db']
}
]
}

const data = JSON.stringify(this.selectedUserPlaylist) + '\n'

// See data-settings.js `promptAndWriteToFile`
const response = await showSaveDialog(options)
if (response.canceled || response.filePath === '') {
// User canceled the save dialog
return
}

try {
await writeFileFromDialog(response, data)
} catch (writeErr) {
const message = this.$t('Settings.Data Settings.Unable to write file')
showToast(`${message}: ${writeErr}`)
return
}

showToast(this.$t('User Playlists.The playlist has been successfully exported'))
},

exitEditMode: function () {
this.editMode = false

Expand Down
7 changes: 7 additions & 0 deletions src/renderer/components/playlist-info/playlist-info.vue
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@
theme="secondary"
@click="toggleCopyVideosPrompt"
/>
<ft-icon-button
v-if="exportPlaylistButtonVisible"
:title="$t('User Playlists.Export Playlist')"
:icon="['fas', 'file-arrow-down']"
theme="secondary"
@click="handlePlaylistExport"
/>
<ft-icon-button
v-if="!editMode && userPlaylistDuplicateItemCount > 0"
:title="$t('User Playlists.Remove Duplicate Videos')"
Expand Down
2 changes: 2 additions & 0 deletions static/locales/en-US.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ User Playlists:
Remove Watched Videos: Remove Watched Videos
Enable Quick Bookmark With This Playlist: Enable Quick Bookmark With This Playlist
Quick Bookmark Enabled: Quick Bookmark Enabled
Export Playlist: Export This Playlist
The playlist has been successfully exported: The playlist has been successfully exported
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.
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.
Delete Playlist: Delete Playlist
Expand Down

0 comments on commit d3b6563

Please sign in to comment.