Skip to content
Merged

Dev #3013

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
126 changes: 126 additions & 0 deletions app/Console/Commands/IngestCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Str;
use App\ResourceItem;
use Storage;

// Usage:
// php artisan ingest {path} {type} {id?} [--save]
// type example: resources.pdf
class IngestCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'ingest
{path : Absolute or relative file path}
{type : Namespace.subtype, e.g. resources.pdf}
{id? : Object id if --save is used}
{--save : Persist to object instead of just returning URL}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Ingest a file by type, optionally persisting to an object.';

/**
* Execute the console command.
*/
public function handle(): int
{
$path = $this->argument('path');
$id = $this->argument('id');
$type = strtolower($this->argument('type'));
$save = (bool) $this->option('save');

if (!is_file($path)) {
$this->error("File not found: {$path}");
return self::FAILURE;
}

[$namespace, $subtype] = array_pad(explode('.', $type, 2), 2, null);
if (!$namespace || !$subtype) {
$this->error("Invalid type. Expected namespace.subtype, got '{$type}'");
return self::FAILURE;
}

$disk = $this->resolveDisk($namespace, $subtype);
if (!$disk) {
$this->error("No storage disk resolver for type '{$type}'");
return self::FAILURE;
}

$url = match ("{$namespace}.{$subtype}") {
'resources.pdf' => $this->handleResourcesPdf($path, $disk),
default => null,
};

if (!$url) {
$this->error("Unsupported type '{$type}'");
return self::FAILURE;
}

if ($save) {
if (!$id) {
$this->error("--save requires an {id}");
return self::FAILURE;
}
$persisted = $this->persistToObject($namespace, $subtype, (int) $id, $url);
if (!$persisted) {
$this->error("Failed to persist URL to object for type '{$type}' and id {$id}");
return self::FAILURE;
}
}

$this->line($url);
return self::SUCCESS;
}

protected function resolveDisk(string $namespace, string $subtype): ?string
{
return match ($namespace) {
'resources' => 'resources',
default => null,
};
}

protected function handleResourcesPdf(string $path, string $disk): ?string
{
$ext = strtolower(pathinfo($path, PATHINFO_EXTENSION) ?: 'pdf');
$safe = Str::slug(pathinfo($path, PATHINFO_FILENAME)) ?: 'document';
$name = "{$safe}-" . time() . ".{$ext}";

$contents = @file_get_contents($path);
if ($contents === false) {
return null;
}

Storage::disk($disk)->put($name, $contents);
return Storage::disk($disk)->url($name);
}

protected function persistToObject(string $namespace, string $subtype, int $id, string $url): bool
{
return match ("{$namespace}.{$subtype}") {
'resources.pdf' => $this->persistResourceSource($id, $url),
default => false,
};
}

protected function persistResourceSource(int $id, string $url): bool
{
$item = ResourceItem::find($id);
if (!$item) {
return false;
}
$item->source = $url;
return (bool) $item->save();
}
}
237 changes: 0 additions & 237 deletions public/build/assets/app-DCD9tGFe.js

This file was deleted.

1 change: 0 additions & 1 deletion public/build/assets/app-DmgLUX1H.css

This file was deleted.

Large diffs are not rendered by default.

237 changes: 237 additions & 0 deletions public/build/assets/app-O6mVr_la.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions public/build/assets/app-nj4-bsDu.css

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

54 changes: 27 additions & 27 deletions public/build/manifest.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"resources/assets/sass/app.scss": {
"file": "assets/app-y4d-lMwj.css",
"file": "assets/app-DmtIZ-J6.css",
"src": "resources/assets/sass/app.scss",
"isEntry": true
},
"resources/css/app.css": {
"file": "assets/app-DmgLUX1H.css",
"file": "assets/app-nj4-bsDu.css",
"src": "resources/css/app.css",
"isEntry": true
},
"resources/js/app.js": {
"file": "assets/app-DCD9tGFe.js",
"file": "assets/app-O6mVr_la.js",
"name": "app",
"src": "resources/js/app.js",
"isEntry": true,
Expand Down Expand Up @@ -51,7 +51,7 @@
]
},
"resources/lang/php_al.json": {
"file": "assets/php_al-BjwTomF1.js",
"file": "assets/php_al-D0vHDga_.js",
"name": "php_al",
"src": "resources/lang/php_al.json",
"isDynamicEntry": true
Expand All @@ -63,43 +63,43 @@
"isDynamicEntry": true
},
"resources/lang/php_bg.json": {
"file": "assets/php_bg-Cn6UmBoh.js",
"file": "assets/php_bg-CaYn18xF.js",
"name": "php_bg",
"src": "resources/lang/php_bg.json",
"isDynamicEntry": true
},
"resources/lang/php_cs.json": {
"file": "assets/php_cs-BIv3xhQw.js",
"file": "assets/php_cs-CbHtG3Mv.js",
"name": "php_cs",
"src": "resources/lang/php_cs.json",
"isDynamicEntry": true
},
"resources/lang/php_da.json": {
"file": "assets/php_da-DYfpOnGQ.js",
"file": "assets/php_da-COQa__5e.js",
"name": "php_da",
"src": "resources/lang/php_da.json",
"isDynamicEntry": true
},
"resources/lang/php_de.json": {
"file": "assets/php_de-B49h_8nv.js",
"file": "assets/php_de-BlZpyzns.js",
"name": "php_de",
"src": "resources/lang/php_de.json",
"isDynamicEntry": true
},
"resources/lang/php_el.json": {
"file": "assets/php_el-V_dVO9S6.js",
"file": "assets/php_el-D72kitKn.js",
"name": "php_el",
"src": "resources/lang/php_el.json",
"isDynamicEntry": true
},
"resources/lang/php_en.json": {
"file": "assets/php_en-DIv06PNk.js",
"file": "assets/php_en-Bk2sLkF9.js",
"name": "php_en",
"src": "resources/lang/php_en.json",
"isDynamicEntry": true
},
"resources/lang/php_es.json": {
"file": "assets/php_es-CBjAegpg.js",
"file": "assets/php_es-Cv4rswUn.js",
"name": "php_es",
"src": "resources/lang/php_es.json",
"isDynamicEntry": true
Expand All @@ -111,19 +111,19 @@
"isDynamicEntry": true
},
"resources/lang/php_fi.json": {
"file": "assets/php_fi-BdkyMVsi.js",
"file": "assets/php_fi-N_0nutrm.js",
"name": "php_fi",
"src": "resources/lang/php_fi.json",
"isDynamicEntry": true
},
"resources/lang/php_fr.json": {
"file": "assets/php_fr-DjBUbDPE.js",
"file": "assets/php_fr-UNtU_0Kg.js",
"name": "php_fr",
"src": "resources/lang/php_fr.json",
"isDynamicEntry": true
},
"resources/lang/php_hr.json": {
"file": "assets/php_hr-DmHztw9E.js",
"file": "assets/php_hr-HEcdrWkW.js",
"name": "php_hr",
"src": "resources/lang/php_hr.json",
"isDynamicEntry": true
Expand All @@ -135,19 +135,19 @@
"isDynamicEntry": true
},
"resources/lang/php_it.json": {
"file": "assets/php_it-C6MxjLu7.js",
"file": "assets/php_it-DSdV31u0.js",
"name": "php_it",
"src": "resources/lang/php_it.json",
"isDynamicEntry": true
},
"resources/lang/php_lt.json": {
"file": "assets/php_lt-CEPGvss1.js",
"file": "assets/php_lt-EZbtlyJZ.js",
"name": "php_lt",
"src": "resources/lang/php_lt.json",
"isDynamicEntry": true
},
"resources/lang/php_lv.json": {
"file": "assets/php_lv-DPS-gfUn.js",
"file": "assets/php_lv-B0ay8Kyt.js",
"name": "php_lv",
"src": "resources/lang/php_lv.json",
"isDynamicEntry": true
Expand All @@ -165,31 +165,31 @@
"isDynamicEntry": true
},
"resources/lang/php_mt.json": {
"file": "assets/php_mt-N_XQkU9B.js",
"file": "assets/php_mt-BLsou_cX.js",
"name": "php_mt",
"src": "resources/lang/php_mt.json",
"isDynamicEntry": true
},
"resources/lang/php_nl.json": {
"file": "assets/php_nl-BXfby3eY.js",
"file": "assets/php_nl-BDqDaQDK.js",
"name": "php_nl",
"src": "resources/lang/php_nl.json",
"isDynamicEntry": true
},
"resources/lang/php_pl.json": {
"file": "assets/php_pl-CFP-ihjt.js",
"file": "assets/php_pl-CeSvBt9l.js",
"name": "php_pl",
"src": "resources/lang/php_pl.json",
"isDynamicEntry": true
},
"resources/lang/php_pt.json": {
"file": "assets/php_pt-i32t06-b.js",
"file": "assets/php_pt-DgcPlj0q.js",
"name": "php_pt",
"src": "resources/lang/php_pt.json",
"isDynamicEntry": true
},
"resources/lang/php_ro.json": {
"file": "assets/php_ro-CQJKC5gH.js",
"file": "assets/php_ro-D-D0kZBd.js",
"name": "php_ro",
"src": "resources/lang/php_ro.json",
"isDynamicEntry": true
Expand All @@ -201,31 +201,31 @@
"isDynamicEntry": true
},
"resources/lang/php_sk.json": {
"file": "assets/php_sk-BdcVh0gx.js",
"file": "assets/php_sk-ZtFhB5Su.js",
"name": "php_sk",
"src": "resources/lang/php_sk.json",
"isDynamicEntry": true
},
"resources/lang/php_sl.json": {
"file": "assets/php_sl-C8zYwZcw.js",
"file": "assets/php_sl-VXTkbTOG.js",
"name": "php_sl",
"src": "resources/lang/php_sl.json",
"isDynamicEntry": true
},
"resources/lang/php_sv.json": {
"file": "assets/php_sv-CAch7Ndi.js",
"file": "assets/php_sv-DN1F6LoF.js",
"name": "php_sv",
"src": "resources/lang/php_sv.json",
"isDynamicEntry": true
},
"resources/lang/php_tr.json": {
"file": "assets/php_tr-ChmhwO41.js",
"file": "assets/php_tr-lPflqMaR.js",
"name": "php_tr",
"src": "resources/lang/php_tr.json",
"isDynamicEntry": true
},
"resources/lang/php_ua.json": {
"file": "assets/php_ua-C0cpE4dP.js",
"file": "assets/php_ua-CWZJb9a5.js",
"name": "php_ua",
"src": "resources/lang/php_ua.json",
"isDynamicEntry": true
Expand Down
Loading
Loading