Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store mp3 preview files (fixes #66) #73

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
if defined? VagrantPlugins::HostsUpdater
config.hostsupdater.aliases = settings['sites'].map { |site| site['map'] }
end

config.vm.provision 'ffmpeg', type: 'shell', inline: 'apt install -y ffmpeg'
end
18 changes: 17 additions & 1 deletion app/SongComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,25 @@ protected function createOrUpdate(array $metadata, string $file): array
Storage::disk()->makeDirectory('public/songs/' . $song->id);
}

// Store song image.
File::move($file, storage_path('app/public/songs') . "/{$song->id}/{$song->id}-{$songDetails->id}.zip");
Storage::disk()->put("public/songs/{$song->id}/{$song->id}-{$songDetails->id}.{$songData['coverType']}", base64_decode($songData['coverData']));

// Create MP3 previews (e.g., for iOS).
foreach ($songData['songPreviews'] as $audioPath => $songPreviewData) {
// Save original song data temporarily to convert with ffmpeg.
$audioFilename = basename($audioPath);
$storagePathBase = "public/songs/{$song->id}";
$storagePathPreview = "{$storagePathBase}/temp-{$audioFilename}";
Storage::disk()->put("$storagePathPreview", base64_decode($songPreviewData));

// Convert to MP3 with ffmpeg.
$audioFilenameBase = pathinfo($audioFilename)['filename'];
$previewOutPath = storage_path("app/{$storagePathBase}/{$audioFilenameBase}.mp3");
$previewTempPath = storage_path("app/{$storagePathPreview}");
exec("(ffmpeg -i \"{$previewTempPath}\" \"{$previewOutPath}\"; rm \"{$previewTempPath}\") > /dev/null &");
}

return [
'status' => static::SONG_CREATED,
'key' => $song->id . '-' . $songDetails->id,
Expand Down Expand Up @@ -448,4 +464,4 @@ protected function updateCache(array $song)
}
}

}
}
9 changes: 8 additions & 1 deletion app/UploadParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,16 @@ function map($v)
}

$hashBase = '';
$songData['songPreviews'] = [];
foreach ($info['difficultyLevels'] as $difficultyLevel) {

if ($this->zipHasFile($difficultyLevel['audioPath']) && $this->zipHasFile($difficultyLevel['jsonPath'])) {
// Store song preview data in object for SongComposer to save to disk.
$audioPath = $difficultyLevel['audioPath'];
if (!array_key_exists($audioPath, $songData['songPreviews'])) {
$songData['songPreviews'][$audioPath] = base64_encode($this->readFromZip($audioPath));
}

$songData['difficultyLevels'][$difficultyLevel['difficulty']] = [
'difficulty' => $difficultyLevel['difficulty'],
'rank' => $difficultyLevel['difficultyRank'],
Expand Down Expand Up @@ -290,4 +297,4 @@ protected function closeZip()
$this->zipFile = null;
}
}
}
}