implement refeshTrackCache

This commit is contained in:
SrGooglo 2023-05-31 01:39:32 +00:00
parent 89085b9b6a
commit e686bf31ad
3 changed files with 81 additions and 5 deletions

View File

@ -4,6 +4,8 @@ import classnames from "classnames"
import { DragDropContext, Droppable, Draggable } from "react-beautiful-dnd"
import UploadButton from "components/UploadButton"
import PlaylistModel from "models/playlists"
import { Icons } from "components/Icons"
import "./index.less"
@ -28,6 +30,10 @@ const FileItemEditor = (props) => {
})
}
const onRefreshCache = () => {
props.onRefreshCache(track)
}
const onClose = () => {
if (typeof props.close === "function") {
props.close()
@ -50,19 +56,19 @@ const FileItemEditor = (props) => {
</div>
<div className="fileItemEditor_field_thumnail">
<img src={track.thumbnail} />
<img src={track.cover ?? track.thumbnail} />
</div>
<div className="fileItemEditor_actions">
<UploadButton
accept="image/*"
onUploadDone={(file) => handleChange("thumbnail", file.url)}
onUploadDone={(file) => handleChange("cover", file.url)}
/>
{
track.thumbnail && <antd.Button
(track.cover ?? track.thumbnail) && <antd.Button
icon={<Icons.MdClose />}
type="text"
onClick={() => handleChange("thumbnail", null)}
onClick={() => handleChange("cover", null)}
>
Remove
</antd.Button>
@ -149,6 +155,15 @@ const FileItemEditor = (props) => {
</div>
<div className="fileItemEditor_actions">
{
track._id && <antd.Button
type="text"
icon={<Icons.MdRefresh />}
onClick={onRefreshCache}
>
Refresh Cache
</antd.Button>
}
<antd.Button
type="text"
icon={<Icons.MdClose />}
@ -194,7 +209,7 @@ const FileListItem = (props) => {
<div className="fileListItem_cover">
<img
src={props.track?.thumbnail}
src={props.track.cover ?? props.track?.thumbnail}
alt="Track cover"
/>
</div>
@ -276,12 +291,27 @@ export default (props) => {
const onClickEditTrack = (track) => {
app.DrawerController.open("track_editor", FileItemEditor, {
type: "drawer",
props: {
width: "25vw",
minWidth: "500px",
},
componentProps: {
track,
onSave: (newTrackData) => {
console.log("Saving track", newTrackData)
props.handleTrackInfoChange(newTrackData.uid, newTrackData)
},
onRefreshCache: () => {
console.log("Refreshing cache for track", track.uid)
PlaylistModel.refreshTrackCache(track._id)
.catch(() => {
app.message.error("Failed to refresh cache for track")
})
.then(() => {
app.message.success("Successfully refreshed cache for track")
})
}
},
})

View File

@ -5,6 +5,20 @@ export default class PlaylistsModel {
return globalThis.__comty_shared_state.instances["music"]
}
static refreshTrackCache = async (track_id) => {
if (!track_id) {
throw new Error("Track ID is required")
}
const { data } = await request({
instance: PlaylistsModel.api_instance,
method: "POST",
url: `/tracks/${track_id}/refresh-cache`,
})
return data
}
static putPlaylist = async (payload) => {
if (!payload) {
throw new Error("Payload is required")

View File

@ -0,0 +1,32 @@
import { Track } from "@models"
import { NotFoundError } from "@classes/Errors"
import getEnhancedLyricsFromTrack from "@services/getEnhancedLyricsFromTrack"
export default async (req, res) => {
const { track_id } = req.params
let track = await Track.findOne({
_id: track_id,
public: true,
}).catch((err) => {
return null
})
if (!track) {
return new NotFoundError(req, res, "Track not found")
}
if (track.lyricsEnabled) {
const enhancedLyrics = await getEnhancedLyricsFromTrack(track, { req }).catch((err) => {
return false
})
if (enhancedLyrics) {
await global.redis.set(`lyrics:${track._id.toString()}`, JSON.stringify(enhancedLyrics), "EX", 60 * 60 * 24 * 30)
}
}
return res.json({
success: true,
})
}