Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async refresh ({ state, commit, dispatch, rootState }, selectedFolders) {
let folders = selectedFolders && selectedFolders.length ? selectedFolders : state.exportFolders
let songs = []
for (let folder of folders) {
try {
const dirs = fs.readdirSync(folder)
for (let item of dirs) {
const pathname = path.join(folder, item)
const stat = fs.statSync(pathname)
if (stat.isFile()) {
if (item.endsWith('.mp3')) {
const metadata = await mm.parseFile(pathname, {
duration: true
})
let songname = item.substring(0, item.lastIndexOf('.')).trim()
let artist = [], name = songname, matched = false
if (songname.split('-')[0] && songname.split('-')[1]) {
artist = songname.split('-')[0].split(',').map(item => { return {name: item} })
name = songname.split('-')[1].trim()
matched = false
}
let extraItem = {
artist, name, matched
}
// console.log(name, artist, metadata)
const songItem = {
function _parseCommon(filename) {
return musicMetadata.parseFile(filename, {native: false})
.then(function(metadata) {
// console.log('metadata._parseCommon', JSON.stringify(metadata));
// console.log('%s - %s\n\tat %s', metadata.common.album, metadata.common.title, filename);
return mm2custom(filename, metadata);
});
}
fs.readdir(this.dir, (err, dir) => {
// loop through results
for (let filePath of dir) {
// ensure that we're only working with files
if (fs.statSync(this.dir + filePath).isFile()) {
let buffer = readChunk.sync(this.dir + filePath, 0, fileType.minimumBytes)
let fileTypeInfo = fileType(buffer)
// only interestedin MP3 files at the moment, ignore all others
if (fileTypeInfo != null) {
if (fileTypeInfo.ext === 'mp3' || fileTypeInfo.ext === 'wav' || fileTypeInfo.ext === 'flac') {
// read metadata
mm.parseFile(this.dir + filePath, {native: true})
.then(metadata => {
console.log(metadata)
// Get data for file object
let artist = metadata.common.artist
let title = metadata.common.title
let album = metadata.common.album
let bitrate = metadata.format.bitrate
let codec = metadata.format.codec
let trackNo = metadata.common.track.no
// write the relevent data to the files array
this.files.push({
fileName: filePath,
artist: (artist !== null) ? artist.substring(0, 50).replace('/', '_') : '',
title: (title !== null) ? title.substring(0, 50).replace('/', '_') : '',
album: (album !== null) ? album : '',
trackNo: (trackNo !== null) ? trackNo : 0,
export const getMetadata = async (trackPath: string): Promise<track> => {
const defaultMetadata = getDefaultMetadata();
const basicMetadata: Track = {
...defaultMetadata,
path: trackPath
};
try {
const stats = await stat(trackPath);
const data = await mmd.parseFile(trackPath, {
native: true, skipCovers: true, fileSize: stats.size, duration: true
});
// Let's try to define something with what we got so far...
const parsedData = parseMusicMetadata(data, trackPath);
const metadata: Track = {
...defaultMetadata,
...parsedData,
path: trackPath
};
metadata.loweredMetas = getLoweredMeta(metadata);
// Let's try another wat to retrieve a track duration
if (!metadata.duration) {
async process (item, pathId) {
const pathInfo = path.parse(item.file)
const tags = await musicMeta.parseFile(item.file, {
duration: true,
skipCovers: true,
})
if (!tags.format.duration) {
throw new Error(' => could not determine duration')
}
log.info(' => duration: %s:%s',
Math.floor(tags.format.duration / 60),
Math.round(tags.format.duration % 60, 10).toString().padStart(2, '0')
)
// run parser
const parsed = this.parser({
dir: pathInfo.dir,
detectBPM() {
if (this.props.isFirst && this.props.audio.url) {
mm.parseFile(urlToPath(this.props.audio.url))
.then((metadata: any) => {
if (metadata && metadata.common && metadata.common.bpm) {
this.changeKey('bpm', metadata.common.bpm);
}
})
.catch((err: any) => {
console.error("Error reading metadata:", err.message);
});
}
}
return new Promise((resolve, reject) => {
return MetadataParser.parseFile(filePath, { duration: true })
.then((metadata: IAudioMetadata) => {
const imageFilePath = saveImageFile(metadata.common.picture)
const common = metadata.common
resolve({
filePath,
artist: common.artist || '',
album: common.album || '',
title: common.title || '',
year: common.year ? String(common.year) : '',
track: common.track && 0 < common.track.no ? common.track.no : 1,
disc: common.disk && 0 < common.disk.no ? common.disk.no : 1,
genre: common.genre && 0 < common.genre.length ? common.genre[0] : '',
duration: metadata.format.duration || 0,
imageFilePath
})
})
_readMetadata (filePath) {
return MusicMetadata.parseFile(filePath, { duration: true })
.then(metadata => {
return { metadata: metadata }
})
}
files.map(async (file, index) => {
const data: IAudioMetadata = await parseFile(file, {
skipCovers: true,
});
const filestats: fs.Stats = await fstat(file);
if (
data.common.title === '' ||
data.common.title === undefined
) {
data.common.title = path.basename(file, path.extname(file));
}
if (
data.common.artist === '' ||
data.common.artist === undefined
) {