Add track cover extraction and upload support

This commit is contained in:
SrGooglo 2025-04-24 07:44:50 +00:00
parent 8b76dfc364
commit 87f750cc8b
2 changed files with 27 additions and 35 deletions

View File

@ -176,8 +176,7 @@ class TracksManager extends React.Component {
const trackManifest = new TrackManifest({ const trackManifest = new TrackManifest({
uid: uid, uid: uid,
file: change.file, file: change.file.originFileObj,
onChange: this.modifyTrackByUid,
}) })
this.addTrackToList(trackManifest) this.addTrackToList(trackManifest)
@ -205,6 +204,23 @@ class TracksManager extends React.Component {
trackManifest.source = change.file.response.url trackManifest.source = change.file.response.url
trackManifest = await trackManifest.initialize() trackManifest = await trackManifest.initialize()
// if has a cover, Upload
if (trackManifest._coverBlob) {
console.log(
`[${trackManifest.uid}] Founded cover, uploading...`,
)
const coverFile = new File(
[trackManifest._coverBlob],
"cover.jpg",
{ type: trackManifest._coverBlob.type },
)
const coverUpload =
await app.cores.remoteStorage.uploadFile(coverFile)
trackManifest.cover = coverUpload.url
}
await this.modifyTrackByUid(uid, trackManifest) await this.modifyTrackByUid(uid, trackManifest)
break break

View File

@ -51,33 +51,28 @@ export default class TrackManifest {
_id = null // used for api requests _id = null // used for api requests
uid = null // used for internal uid = null // used for internal
cover =
"https://storage.ragestudio.net/comty-static-assets/default_song.png"
title = "Untitled" title = "Untitled"
album = "Unknown" album = "Unknown"
artist = "Unknown" artist = "Unknown"
cover = null // set default cover url
source = null source = null
metadata = {} metadata = {}
// set default service to default // set default service to default
service = "default" service = "default"
// Extended from db
liked = null
async initialize() { async initialize() {
if (!this.params.file) { if (!this.params.file) {
return this return this
} }
const analyzedMetadata = await parseBlob( const analyzedMetadata = await parseBlob(this.params.file, {
this.params.file.originFileObj, skipPostHeaders: true,
{ }).catch(() => ({}))
skipPostHeaders: true,
},
).catch(() => ({}))
this.metadata.format = analyzedMetadata.format.codec if (analyzedMetadata.format) {
this.metadata.format = analyzedMetadata.format.codec
}
if (analyzedMetadata.common) { if (analyzedMetadata.common) {
this.title = analyzedMetadata.common.title ?? this.title this.title = analyzedMetadata.common.title ?? this.title
@ -88,32 +83,13 @@ export default class TrackManifest {
if (analyzedMetadata.common.picture) { if (analyzedMetadata.common.picture) {
const cover = analyzedMetadata.common.picture[0] const cover = analyzedMetadata.common.picture[0]
const coverFile = new File([cover.data], "cover", { this._coverBlob = new Blob([cover.data], { type: cover.format })
type: cover.format, this.cover = URL.createObjectURL(this._coverBlob)
})
const coverUpload =
await app.cores.remoteStorage.uploadFile(coverFile)
this.cover = coverUpload.url
} }
this.handleChanges({
cover: this.cover,
title: this.title,
artist: this.artist,
album: this.album,
})
return this return this
} }
handleChanges = (changes) => {
if (typeof this.params.onChange === "function") {
this.params.onChange(this.uid, changes)
}
}
analyzeCoverColor = async () => { analyzeCoverColor = async () => {
const fac = new FastAverageColor() const fac = new FastAverageColor()