fix(backend): avoid duplicate key errors during complex matching by upserting MusicFile by s3Key
This commit is contained in:
parent
1560e614dc
commit
fe3a7abf32
@ -361,16 +361,17 @@ class BackgroundJobService {
|
||||
const fileBuffer = await s3Service.getFileContent(s3File.key);
|
||||
const metadata = await audioMetadataService.extractMetadata(fileBuffer, filename);
|
||||
|
||||
const musicFile = new MusicFile({
|
||||
originalName: filename,
|
||||
s3Key: s3File.key,
|
||||
s3Url: `${process.env.S3_ENDPOINT}/${process.env.S3_BUCKET_NAME}/${s3File.key}`,
|
||||
contentType: guessContentType(filename),
|
||||
size: s3File.size,
|
||||
...metadata,
|
||||
});
|
||||
|
||||
processedFiles.push(musicFile);
|
||||
// Reuse existing MusicFile document if present to avoid duplicate key errors
|
||||
let musicFile = await MusicFile.findOne({ s3Key: s3File.key });
|
||||
if (!musicFile) {
|
||||
musicFile = new MusicFile({ s3Key: s3File.key });
|
||||
}
|
||||
musicFile.originalName = filename;
|
||||
musicFile.s3Key = s3File.key;
|
||||
musicFile.s3Url = `${process.env.S3_ENDPOINT}/${process.env.S3_BUCKET_NAME}/${s3File.key}`;
|
||||
musicFile.contentType = guessContentType(filename);
|
||||
musicFile.size = s3File.size;
|
||||
Object.assign(musicFile, metadata);
|
||||
|
||||
// Try complex matching
|
||||
const matchResult = await songMatchingService.matchMusicFileToSongs(musicFile, {
|
||||
@ -402,7 +403,7 @@ class BackgroundJobService {
|
||||
stillUnmatched++;
|
||||
}
|
||||
|
||||
// Save immediately for real-time availability
|
||||
// Save immediately for real-time availability (create or update)
|
||||
await musicFile.save();
|
||||
processedFiles.push(musicFile);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user