Skip to content

Commit 8841bd2

Browse files
author
Josef Citrine
committed
#100: Progress commit
1 parent 3526403 commit 8841bd2

File tree

2 files changed

+118
-15
lines changed

2 files changed

+118
-15
lines changed

app/Console/Commands/ImportPonify.php

Lines changed: 70 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,6 @@ public function handle()
140140

141141
$getId3 = new getID3;
142142

143-
// Enable file hashing
144-
$getId3->option_md5_data = true;
145-
$getId3->option_md5_data_source = true;
146-
147143
// all tags read by getID3, including the cover art
148144
$allTags = $getId3->analyze($file->getPathname());
149145

@@ -153,7 +149,11 @@ public function handle()
153149
// normalized tags used by Pony.fm
154150
$parsedTags = [];
155151

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));
157157

158158
//==========================================================================================================
159159
// Check to see if we have this track already, if so, compare hashes of the two files
@@ -194,18 +194,46 @@ public function handle()
194194

195195
if ($existingFile === null) {
196196
// 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;
198217

199218
} else {
200219
$this->comment("Found existing file");
201220

202221
// 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+
203231
$getId3_existing = new getID3;
204232
$getId3_existing->option_md5_data = true;
205233
$getId3_existing->option_md5_data_source = true;
206-
$existingFileTags = $getId3->analyze($existingFile->getFile());
234+
$existingFileTags = $getId3_existing->analyze($existingFile->getFile());
207235

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'];
209237
$targetHash = array_key_exists('md5_data_source', $existingFileTags) ? $existingFileTags['md5_data_source'] : $existingFileTags['md5_data'];
210238

211239
$this->info("Archive hash: " . $importHash);
@@ -218,8 +246,12 @@ public function handle()
218246
$this->comment("Versions are the same. Skipping...\n");
219247
continue;
220248
} 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;
223255
}
224256
}
225257
} else {
@@ -253,9 +285,15 @@ public function handle()
253285

254286
$this->comment('Extracting cover art!');
255287
$coverId = null;
288+
$image = null;
289+
256290
if (array_key_exists('comments', $allTags) && array_key_exists('picture', $allTags['comments'])) {
257291
$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+
}
258295

296+
if ($image !== null) {
259297
if ($image['image_mime'] === 'image/png') {
260298
$extension = 'png';
261299
} else {
@@ -300,6 +338,7 @@ public function handle()
300338
$this->error(json_encode($result->getValidator()->messages()->getMessages(), JSON_PRETTY_PRINT));
301339
} else {
302340
$track = Track::find($result->getResponse()['id']);
341+
$track->cover_id = $coverId;
303342
$track->license_id = 2;
304343
$track->save();
305344
}
@@ -324,17 +363,13 @@ protected function getFormat($extension) {
324363
return null;
325364
}
326365

327-
public function parseTags($file)
366+
public function parseTags($file, $allTags)
328367
{
329368
$audioCodec = $file->getExtension();
330369

331370
//==========================================================================================================
332371
// Extract the original tags.
333372
//==========================================================================================================
334-
$getId3 = new getID3;
335-
336-
// all tags read by getID3, including the cover art
337-
$allTags = $getId3->analyze($file->getPathname());
338373

339374
// $rawTags => tags specific to a file format (ID3 or Atom), pre-normalization but with cover art removed
340375
// $parsedTags => normalized tags used by Pony.fm
@@ -376,6 +411,8 @@ protected function getId3Tags($rawTags)
376411
{
377412
if (array_key_exists('tags', $rawTags) && array_key_exists('id3v2', $rawTags['tags'])) {
378413
$tags = $rawTags['tags']['id3v2'];
414+
} elseif (array_key_exists('id3v2', $rawTags)) {
415+
$tags = $rawTags['tags']['id3v2'];
379416
} elseif (array_key_exists('tags', $rawTags) && array_key_exists('id3v1', $rawTags['tags'])) {
380417
$tags = $rawTags['tags']['id3v1'];
381418
} else {
@@ -555,4 +592,22 @@ protected function parseDateString(string $dateString)
555592
}
556593
}
557594
}
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+
}
558613
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class CreatePonifyTracks extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('ponify_tracks', function(Blueprint $table)
17+
{
18+
$table->increments('id');
19+
$table->integer('track_id')->unsigned()->index();
20+
$table->string('path')->index();
21+
$table->string('filename')->index();
22+
$table->string('extension')->index();
23+
$table->dateTime('imported_at');
24+
$table->text('parsed_tags');
25+
$table->text('raw_tags');
26+
});
27+
28+
Schema::table('ponify_tracks', function(Blueprint $table)
29+
{
30+
$table->foreign('track_id')->references('id')->on('tracks')->onUpdate('RESTRICT')->onDelete('RESTRICT');
31+
});
32+
}
33+
34+
/**
35+
* Reverse the migrations.
36+
*
37+
* @return void
38+
*/
39+
public function down()
40+
{
41+
Schema::table('ponify_tracks', function(Blueprint $table)
42+
{
43+
$table->dropForeign('ponify_tracks_track_id_foreign');
44+
});
45+
46+
Schema::drop('ponify_tracks');
47+
}
48+
}

0 commit comments

Comments
 (0)