Skip to content

Commit

Permalink
feat: get music name from filename automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
mebtte committed Jul 21, 2024
1 parent cf53ed7 commit 1243aac
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import playerEventemitter, {
} from '../../../eventemitter';
import { Singer } from './constants';
import upperCaseFirstLetter from '#/utils/upper_case_first_letter';
import { base64ToCover, canAudioPlay } from './utils';
import { base64ToCover, canAudioPlay, getMusicNameFromFilename } from './utils';

const maskProps: { style: CSSProperties } = {
style: { zIndex: ZIndex.DIALOG },
Expand Down Expand Up @@ -103,8 +103,8 @@ function CreateMusicDialog() {
getMusicFileMetadata(a)
.then((metadata) => {
const { title, artist } = metadata;
if (!name && title) {
setName(title);
if (!name) {
setName(title || getMusicNameFromFilename(a.name));
}
if (!singerList.length && artist) {
searchSingerRequest({
Expand Down Expand Up @@ -155,11 +155,7 @@ function CreateMusicDialog() {
});

try {
const {
// lyric,
picture,
year,
} = await getMusicFileMetadata(asset);
const { picture, year } = await getMusicFileMetadata(asset);
const updateCover = async (pb: string) => {
const coverBlob = await base64ToCover(pb);
const { id: assetId } = await uploadAsset(
Expand All @@ -175,14 +171,6 @@ function CreateMusicDialog() {
};

await Promise.all([
// musicType === MusicType.SONG && lyric
// ? await updateMusic({
// id,
// key: AllowUpdateKey.LYRIC,
// value: [lyric],
// requestMinimalDuration: 0,
// })
// : null,
picture ? updateCover(picture.dataURI) : null,
year
? updateMusic({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,12 @@ export function canAudioPlay(file: File) {
URL.revokeObjectURL(url);
});
}

export function getMusicNameFromFilename(filename: string) {
const lastIndex = filename.lastIndexOf('.');
return lastIndex === -1
? filename
: lastIndex === 0
? ''
: filename.slice(0, lastIndex);
}
10 changes: 1 addition & 9 deletions shared/utils/get_music_file_metadata.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import jsmediatags from 'jsmediatags';

interface Metadata {
// lyric?: string;
title?: string;
artist?: string;
picture?: {
Expand All @@ -21,18 +20,11 @@ function getMusicFileMetadata(file: File | string) {
return new Promise<Metadata>((resolve, reject) => {
jsmediatags.read(file, {
onSuccess: async (metadata) => {
const {
// lyrics,
picture,
title,
artist,
year,
} = metadata.tags;
const { picture, title, artist, year } = metadata.tags;

return resolve({
title,
artist,
// lyric: lyrics,
picture: picture
? {
dataURI: pictureToDataURI(picture),
Expand Down

0 comments on commit 1243aac

Please sign in to comment.