Skip to content

Commit add3cc4

Browse files
committed
test
1 parent 804db1a commit add3cc4

3 files changed

Lines changed: 54 additions & 4 deletions

File tree

app/Console/Commands/ResourcesExportS3Urls.php

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ class ResourcesExportS3Urls extends Command
1313
{
1414
protected $signature = 'resources:export-s3-urls
1515
{--active-only : Only rows with active=1}
16-
{--json : Output JSON array instead of CSV}';
16+
{--json : Output JSON array instead of CSV}
17+
{--output= : Write CSV or JSON to this path (e.g. storage/app/resources_s3_urls.csv)}';
1718

1819
protected $description = 'Export ResourceItem id, name, source, thumbnail (for matching local filenames to live S3 keys)';
1920

@@ -25,8 +26,22 @@ public function handle(): int
2526
}
2627
$rows = $q->get(['id', 'name', 'source', 'thumbnail']);
2728

29+
$outputPath = $this->option('output');
30+
$outputPath = is_string($outputPath) ? trim($outputPath) : '';
31+
2832
if ($this->option('json')) {
29-
$this->line($rows->toJson(JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
33+
$payload = $rows->toJson(JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
34+
if ($outputPath !== '') {
35+
$full = $this->resolveOutputPath($outputPath);
36+
if ($full === null) {
37+
return self::FAILURE;
38+
}
39+
file_put_contents($full, $payload);
40+
$this->info("Wrote JSON ({$rows->count()} items) to: {$full}");
41+
42+
return self::SUCCESS;
43+
}
44+
$this->line($payload);
3045

3146
return self::SUCCESS;
3247
}
@@ -46,9 +61,43 @@ public function handle(): int
4661
]);
4762
}
4863
rewind($out);
49-
$this->output->write(stream_get_contents($out) ?: '');
64+
$csv = stream_get_contents($out) ?: '';
5065
fclose($out);
5166

67+
if ($outputPath !== '') {
68+
$full = $this->resolveOutputPath($outputPath);
69+
if ($full === null) {
70+
return self::FAILURE;
71+
}
72+
file_put_contents($full, $csv);
73+
$this->info("Wrote CSV ({$rows->count()} rows) to: {$full}");
74+
75+
return self::SUCCESS;
76+
}
77+
78+
$this->output->write($csv);
79+
5280
return self::SUCCESS;
5381
}
82+
83+
/**
84+
* @return string|null Absolute path, or null on error
85+
*/
86+
private function resolveOutputPath(string $path): ?string
87+
{
88+
$full = str_starts_with($path, DIRECTORY_SEPARATOR) || preg_match('#^[a-zA-Z]:\\\\#', $path) === 1
89+
? $path
90+
: base_path($path);
91+
92+
$dir = dirname($full);
93+
if (! is_dir($dir)) {
94+
if (! @mkdir($dir, 0755, true) && ! is_dir($dir)) {
95+
$this->error("Cannot create directory: {$dir}");
96+
97+
return null;
98+
}
99+
}
100+
101+
return $full;
102+
}
54103
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
id,name,source,thumbnail,pdf_basename,thumb_basename

resources/views/admin/resources-import/index.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class="rounded border-gray-300">
4747
<input type="file" name="file" id="file" accept=".csv,.xlsx,.xls" required
4848
class="block w-full max-w-md" aria-required="true">
4949
<p class="text-sm text-gray-600 mt-1">Required column: <code>name_of_the_resource</code>. Optional: link, description, image, filters_type, filters_target_audience, filters_level_of_difficulty, filters_programming_language, filters_subjects, filters_topics, filters_language, category, group_name, <code>s3_suffix</code> (or <code>file_suffix</code>) per row for file naming. Max 10 MB.</p>
50-
<p class="text-sm text-gray-600 mt-1">After verify, the preview step lets you choose <strong>batch</strong> / <strong>stable</strong> / <strong>preserve</strong> naming or a <strong>custom suffix</strong>. On the server: <code>php artisan resources:export-s3-urls &gt; stamps.csv</code> then rename local PDFs/images to match <code>pdf_basename</code> / <code>thumb_basename</code> and import with <code>--preserve-filenames</code>. See <code>resources:import --help</code>.</p>
50+
<p class="text-sm text-gray-600 mt-1">After verify, the preview step lets you choose <strong>batch</strong> / <strong>stable</strong> / <strong>preserve</strong> naming or a <strong>custom suffix</strong>. On the server: <code>php artisan resources:export-s3-urls --output=storage/app/resources_s3_urls.csv</code> then rename local PDFs/images to match <code>pdf_basename</code> / <code>thumb_basename</code> and import with <code>--preserve-filenames</code>. Example header: <code>docs/resources/resources-s3-urls-export.example.csv</code>. See <code>resources:import --help</code>.</p>
5151
<p id="file-required-hint" class="text-sm text-amber-600 mt-1 hidden">Please select a file first, then click Verify.</p>
5252
</div>
5353

0 commit comments

Comments
 (0)