Browse Source

feat(EditPlaylist): playlist 'export as json' functionality

Signed-off-by: Jonathan <theflametrooper@gmail.com>
Jonathan 4 years ago
parent
commit
5e9fbb8a22
2 changed files with 47 additions and 1 deletions
  1. 1 1
      backend/logic/app.js
  2. 46 0
      frontend/src/components/modals/EditPlaylist/index.vue

+ 1 - 1
backend/logic/app.js

@@ -58,7 +58,7 @@ class _AppModule extends CoreClass {
 				})
 				.catch(console.error);
 
-			const corsOptions = { ...config.get("cors") };
+			const corsOptions = { ...config.get("cors"), credentials: true };
 
 			app.use(cors(corsOptions));
 			app.options("*", cors(corsOptions));

+ 46 - 0
frontend/src/components/modals/EditPlaylist/index.vue

@@ -310,6 +310,14 @@
 			 -->
 		</div>
 		<div slot="footer">
+			<a
+				class="button is-default"
+				v-if="this.userId === this.playlist.createdBy"
+				@click="downloadPlaylist()"
+				href="#"
+			>
+				Download Playlist
+			</a>
 			<a
 				class="button is-danger"
 				@click="removePlaylist()"
@@ -344,6 +352,7 @@ export default {
 		return {
 			utils,
 			drag: false,
+			serverDomain: "",
 			playlist: { songs: [] }
 		};
 	},
@@ -564,6 +573,43 @@ export default {
 				}
 			});
 		},
+		async downloadPlaylist() {
+			if (this.serverDomain === "")
+				this.serverDomain = await lofig.get("serverDomain");
+
+			fetch(
+				`${this.serverDomain}/export/privatePlaylist/${this.playlist._id}`,
+				{ credentials: "include" }
+			)
+				.then(res => res.blob())
+				.then(blob => {
+					const url = window.URL.createObjectURL(blob);
+
+					const a = document.createElement("a");
+					a.style.display = "none";
+					a.href = url;
+
+					a.download = `musare-privateplaylist-${
+						this.playlist._id
+					}-${new Date().toISOString()}.json`;
+
+					document.body.appendChild(a);
+					a.click();
+					window.URL.revokeObjectURL(url);
+
+					new Toast({
+						content: "Successfully downloaded playlist.",
+						timeout: 3000
+					});
+				})
+				.catch(
+					() =>
+						new Toast({
+							content: "Failed to export and download playlist.",
+							timeout: 3000
+						})
+				);
+		},
 		moveSongToTop(index) {
 			this.playlist.songs.splice(
 				0,