@@ -140,10 +140,6 @@ public function handle()
140
140
141
141
$ getId3 = new getID3 ;
142
142
143
- // Enable file hashing
144
- $ getId3 ->option_md5_data = true ;
145
- $ getId3 ->option_md5_data_source = true ;
146
-
147
143
// all tags read by getID3, including the cover art
148
144
$ allTags = $ getId3 ->analyze ($ file ->getPathname ());
149
145
@@ -153,7 +149,11 @@ public function handle()
153
149
// normalized tags used by Pony.fm
154
150
$ parsedTags = [];
155
151
156
- list ($ parsedTags , $ rawTags ) = $ this ->parseTags ($ file );
152
+ list ($ parsedTags , $ rawTags ) = $ this ->parseTags ($ file , $ allTags );
153
+
154
+ $ imageFilename = $ file ->getFilename () . ".tags.txt " ;
155
+ $ imageFilePath = "$ tmpPath/ " . $ imageFilename ;
156
+ File::put ($ imageFilePath , print_r ($ allTags , true ));
157
157
158
158
//==========================================================================================================
159
159
// Check to see if we have this track already, if so, compare hashes of the two files
@@ -194,18 +194,46 @@ public function handle()
194
194
195
195
if ($ existingFile === null ) {
196
196
// Can't find a matching format
197
- // Check to see if we have a better quality file
197
+ // Before we do anything, was this from MLPMA?
198
+ $ mlpmaTrack = DB ::table ('mlpma_tracks ' )->where ('track_id ' , '= ' , $ existingTrack ->id )->first ();
199
+
200
+ if (!is_null ($ mlpmaTrack )) {
201
+ // This was from the archive
202
+ // See if we have a higher quality source file
203
+ if (Track::$ Formats [$ importFormat ]['is_lossless ' ]) {
204
+ // Source is lossless, is the existing track lossy?
205
+ if ($ existingFile ->isMasterLossy ()) {
206
+ // Cool! Let's replace it
207
+ $ this ->comment ('Replacing ( ' . $ existingTrack ->id . ') ' . $ existingTrack ->title );
208
+
209
+ $ this ->replaceTrack ($ file , $ existingTrack , $ artist , $ allTags ['mime_type ' ]);
210
+
211
+ continue ;
212
+ }
213
+ }
214
+ }
215
+
216
+ continue ;
198
217
199
218
} else {
200
219
$ this ->comment ("Found existing file " );
201
220
202
221
// Found a matching format, are they the same?
222
+ // Before we check it, see if it came from MLPMA
223
+ // We're only replacing tracks with the same format if they're archived
224
+ $ getId3_source = new getID3 ;
225
+
226
+ $ getId3_source ->option_md5_data = true ;
227
+ $ getId3_source ->option_md5_data_source = true ;
228
+
229
+ $ sourceWithMd5 = $ getId3_source ->analyze ($ file ->getPathname ());
230
+
203
231
$ getId3_existing = new getID3 ;
204
232
$ getId3_existing ->option_md5_data = true ;
205
233
$ getId3_existing ->option_md5_data_source = true ;
206
- $ existingFileTags = $ getId3 ->analyze ($ existingFile ->getFile ());
234
+ $ existingFileTags = $ getId3_existing ->analyze ($ existingFile ->getFile ());
207
235
208
- $ importHash = array_key_exists ('md5_data_source ' , $ allTags ) ? $ allTags ['md5_data_source ' ] : $ allTags ['md5_data ' ];
236
+ $ importHash = array_key_exists ('md5_data_source ' , $ sourceWithMd5 ) ? $ sourceWithMd5 ['md5_data_source ' ] : $ sourceWithMd5 ['md5_data ' ];
209
237
$ targetHash = array_key_exists ('md5_data_source ' , $ existingFileTags ) ? $ existingFileTags ['md5_data_source ' ] : $ existingFileTags ['md5_data ' ];
210
238
211
239
$ this ->info ("Archive hash: " . $ importHash );
@@ -218,8 +246,12 @@ public function handle()
218
246
$ this ->comment ("Versions are the same. Skipping... \n" );
219
247
continue ;
220
248
} else {
221
- // Audio is different. Replace if it came from MLPMA
222
- // TODO: Replace file
249
+ // Audio is different, let's replace it
250
+ $ this ->comment ('Replacing ( ' . $ existingTrack ->id . ') ' . $ existingTrack ->title );
251
+
252
+ $ this ->replaceTrack ($ file , $ existingTrack , $ artist , $ allTags ['mime_type ' ]);
253
+
254
+ continue ;
223
255
}
224
256
}
225
257
} else {
@@ -253,9 +285,15 @@ public function handle()
253
285
254
286
$ this ->comment ('Extracting cover art! ' );
255
287
$ coverId = null ;
288
+ $ image = null ;
289
+
256
290
if (array_key_exists ('comments ' , $ allTags ) && array_key_exists ('picture ' , $ allTags ['comments ' ])) {
257
291
$ image = $ allTags ['comments ' ]['picture ' ][0 ];
292
+ } else if (array_key_exists ('id3v2 ' , $ allTags ) && array_key_exists ('APIC ' , $ allTags ['id3v2 ' ])) {
293
+ $ image = $ allTags ['id3v2 ' ]['APIC ' ][0 ];
294
+ }
258
295
296
+ if ($ image !== null ) {
259
297
if ($ image ['image_mime ' ] === 'image/png ' ) {
260
298
$ extension = 'png ' ;
261
299
} else {
@@ -300,6 +338,7 @@ public function handle()
300
338
$ this ->error (json_encode ($ result ->getValidator ()->messages ()->getMessages (), JSON_PRETTY_PRINT ));
301
339
} else {
302
340
$ track = Track::find ($ result ->getResponse ()['id ' ]);
341
+ $ track ->cover_id = $ coverId ;
303
342
$ track ->license_id = 2 ;
304
343
$ track ->save ();
305
344
}
@@ -324,17 +363,13 @@ protected function getFormat($extension) {
324
363
return null ;
325
364
}
326
365
327
- public function parseTags ($ file )
366
+ public function parseTags ($ file, $ allTags )
328
367
{
329
368
$ audioCodec = $ file ->getExtension ();
330
369
331
370
//==========================================================================================================
332
371
// Extract the original tags.
333
372
//==========================================================================================================
334
- $ getId3 = new getID3 ;
335
-
336
- // all tags read by getID3, including the cover art
337
- $ allTags = $ getId3 ->analyze ($ file ->getPathname ());
338
373
339
374
// $rawTags => tags specific to a file format (ID3 or Atom), pre-normalization but with cover art removed
340
375
// $parsedTags => normalized tags used by Pony.fm
@@ -376,6 +411,8 @@ protected function getId3Tags($rawTags)
376
411
{
377
412
if (array_key_exists ('tags ' , $ rawTags ) && array_key_exists ('id3v2 ' , $ rawTags ['tags ' ])) {
378
413
$ tags = $ rawTags ['tags ' ]['id3v2 ' ];
414
+ } elseif (array_key_exists ('id3v2 ' , $ rawTags )) {
415
+ $ tags = $ rawTags ['tags ' ]['id3v2 ' ];
379
416
} elseif (array_key_exists ('tags ' , $ rawTags ) && array_key_exists ('id3v1 ' , $ rawTags ['tags ' ])) {
380
417
$ tags = $ rawTags ['tags ' ]['id3v1 ' ];
381
418
} else {
@@ -555,4 +592,22 @@ protected function parseDateString(string $dateString)
555
592
}
556
593
}
557
594
}
595
+
596
+ protected function replaceTrack ($ toBeUploaded , $ targetTrack , $ artist , $ mime ) {
597
+ Auth::loginUsingId ($ artist ->id );
598
+
599
+ $ trackFile = new UploadedFile ($ toBeUploaded ->getPathname (), $ toBeUploaded ->getFilename (), $ mime , null , null , true );
600
+
601
+ $ upload = new UploadTrackCommand (true , false , null , false , $ targetTrack ->getNextVersion (), $ targetTrack );
602
+ $ upload ->_file = $ trackFile ;
603
+ $ result = $ upload ->execute ();
604
+
605
+ if ($ result ->didFail ()) {
606
+ $ this ->error (json_encode ($ result ->getValidator ()->messages ()->getMessages (), JSON_PRETTY_PRINT ));
607
+ } else {
608
+ $ track = Track::find ($ result ->getResponse ()['id ' ]);
609
+ $ track ->license_id = 2 ;
610
+ $ track ->save ();
611
+ }
612
+ }
558
613
}
0 commit comments