diff --git a/app/Console/Commands/LibraryMetaDataScan.php b/app/Console/Commands/LibraryMetaDataScan.php new file mode 100644 index 0000000..4b78c04 --- /dev/null +++ b/app/Console/Commands/LibraryMetaDataScan.php @@ -0,0 +1,203 @@ +directories('/media/Downloads'), + $supportedTypes = [ + // Audio + 'mp3', + 'flac', + 'm3u', + 'm4a', + 'ogg', + // ebooks + 'pdf', + 'epub', + //video + 'mp4', + 'mkv', + 'm4v', + 'm2ts', + 'part', + 'mkv.part', + // sub titles + 'ts', + 'srt', + // metadata + 'cue', + 'xml', + // playlist + 'm3u8', + // db + 'json', + 'sqlite', + 'sqlite3', + 'ini', + + // checksums + 'sfv', + 'checksum', + + // audiotracks + 'mka', + ]; + $blacklistedTypes = [ + 'gif', + 'png', + 'jpg', + 'jpeg', + 'nfo', + 'gif', + 'mobileconfig', + 'xci', + 'html', + 'log', + 'svg', + 'txt', + 'url', + 'exe', + 'db', + 'metathumb', + 'iso', + 'lnk', + 'md', + 'About_album', + 'pls', + 'accurip', + 'mid', + 'auCDtect', + 'opus', + 'hosts', + 'last_time', + 'sbk', + 'yml', + 'db-shm', + 'db-wal', + 'zip', + 'yaml', + 'pem', + 'txz', + '1', + 'mbp', + 'env', + 'config', + 'key', + 'js', + 'INFO', + 'css', + 'psd', + 'pspimage', + 'sqlite3-shm', + 'sqlite3-wal', + 'gz', + 'phar', + 'LICENSE', + 'lock', + 'dist', + 'web', + 'php', + 'composer', + 'console', + 'sh', + 'jsonlint', + 'php-cs-fixer', + 'php-parse', + 'phpstan', + 'phpunit', + 'satis', + 'validate-json', + 'var-dump-server', + 'Dockerfile', + 'conf', + 'ico', + 'map', + 'eot', + 'ttf', + 'woff', + 'woff2', + 'twig', + 'meta', + 'PORTING_INFO', + 'compile', + 'bat', + 'stub', + 'scss', + 'rst', + 'diff', + 'phpt', + 'template', + 'y', + 'asc', + 'neon', + 'xsd', + 'tpl', + 'base64', + 'xlf', + 'CHANGELOG', + 'secret', + 'public', + 'planet', + 'port', + 'peer', + ]; + + $this->recursivelyFindFiles('/media/Shows', $supportedTypes, $blacklistedTypes, function (\SplFileInfo $file, $err, $out) { + }); + } + + protected function recursivelyFindFiles(string $directory, array &$supportedTypes, array &$blacklistedTypes, Closure $callback) + { + $this->warn(' Found: '.$directory); + $files = (new Filesystem)->files($directory); + + // Build a list of supported file types. + // Interactively when + /** @var \SplFileInfo $file */ + foreach ($files as $file) { + $name = $file->getBasename(); + $split = explode('.', $name); + $extension = end($split); + $path = $file->getPathname(); + + if (in_array($extension, $blacklistedTypes)) { + continue; + } + + dispatch(new BuildMetaDataFile($path)); + } + + $directories = (new Filesystem)->directories($directory); + + foreach ($directories as $directory) { + $this->recursivelyFindFiles($directory, $supportedTypes, $blacklistedTypes, $callback); + } + } +} diff --git a/app/Jobs/BuildMetaDataFile.php b/app/Jobs/BuildMetaDataFile.php new file mode 100644 index 0000000..d4f6e2a --- /dev/null +++ b/app/Jobs/BuildMetaDataFile.php @@ -0,0 +1,73 @@ +path, + ]); + $process->run(function ($e, $o) { + $file = new \SplFileInfo($this->path); + // Will only be executed on supported types. + $name = $file->getBasename(); + $split = explode('.', $name); + $extension = end($split); + $path = $file->getPathname(); + + $data = json_decode($o, true); + + $data['name'] = $name; + $data['path'] = $path; + // bytes + $data['size'] = $file->getSize(); + $data['type'] = $file->getType(); + + if (! file_exists(storage_path('meta/'.$extension))) { + (new Filesystem) + ->makeDirectory(storage_path('meta/'.$extension), 0755, true); + } + + $metadataNewPath = storage_path('meta/'.$extension.'/'.md5($path).'.json'); + $metadataPath = storage_path('meta/'.md5($path).'.json'); + + if (file_exists($metadataPath)) { + // info('Danger, file conflict in md5 hash', $data); + (new Filesystem)->move($metadataPath, $metadataNewPath); + + return; + } + + file_put_contents($metadataNewPath, json_encode($data, JSON_PRETTY_PRINT)); + }); + } +}