33namespace SolutionForest \InspireCms \Exports \Exporters ;
44
55use Illuminate \Database \Eloquent \Model ;
6+ use Illuminate \Support \Facades \Storage ;
67use Illuminate \Support \Str ;
8+ use SolutionForest \InspireCms \Helpers \FileHelper ;
79use SolutionForest \InspireCms \Helpers \ImportDataHelper ;
810use SolutionForest \InspireCms \Helpers \TemplateHelper ;
911use SolutionForest \InspireCms \ImportData \Entities \Content as ImportEntitiesContent ;
1012use SolutionForest \InspireCms \ImportData \Entities \DocumentType as ImportEntitiesDocumentType ;
1113use SolutionForest \InspireCms \ImportData \Entities \FieldGroup as ImportEntitiesFieldGroup ;
1214use SolutionForest \InspireCms \ImportData \Entities \Language as ImportEntitiesLanguage ;
15+ use SolutionForest \InspireCms \ImportData \Entities \MediaAsset as ImportEntitiesMediaAsset ;
1316use SolutionForest \InspireCms \ImportData \Entities \Navigation as ImportEntitiesNavigation ;
1417use SolutionForest \InspireCms \Models \Contracts \Content ;
1518use SolutionForest \InspireCms \Models \Contracts \DocumentType ;
1619use SolutionForest \InspireCms \Models \Contracts \FieldGroup ;
1720use SolutionForest \InspireCms \Models \Contracts \Language ;
1821use SolutionForest \InspireCms \Models \Contracts \Navigation ;
1922use SolutionForest \InspireCms \Models \Contracts \Template ;
23+ use SolutionForest \InspireCms \Support \Models \Contracts \MediaAsset ;
2024
2125abstract class BaseImportUsedDataExporter extends BaseExporter
2226{
@@ -37,6 +41,9 @@ protected function generateImportFileName(Model $record)
3741 case $ record instanceof FieldGroup:
3842 return Str::replace ('_ ' , '- ' , $ record ->name ) . '.json ' ;
3943
44+ case $ record instanceof MediaAsset:
45+ return $ record ->id . '.json ' ;
46+
4047 case $ record instanceof Template:
4148 if (is_array ($ record ->content )) {
4249 $ themes = array_keys ($ record ->content );
@@ -71,13 +78,6 @@ protected function prepareImportContentFromModel(Model $record)
7178
7279 return json_encode ($ array , JSON_PRETTY_PRINT );
7380
74- // case $record instanceof Template:
75- // $themeContent = $record->content;
76- // if (! is_array($themeContent)) {
77- // $themeContent = [inspirecms_templates()->getCurrentTheme() => $themeContent];
78- // }
79- // return $themeContent;
80-
8181 case $ record instanceof Content:
8282 $ array = ImportEntitiesContent::fromRecord ($ record )->toArray ();
8383
@@ -92,6 +92,11 @@ protected function prepareImportContentFromModel(Model $record)
9292 case $ record instanceof Language:
9393 $ array = ImportEntitiesLanguage::fromRecord ($ record )->toArray ();
9494
95+ return json_encode ($ array , JSON_PRETTY_PRINT );
96+
97+ case $ record instanceof MediaAsset:
98+ $ array = ImportEntitiesMediaAsset::fromRecord ($ record )->toArray ();
99+
95100 return json_encode ($ array , JSON_PRETTY_PRINT );
96101 }
97102
@@ -104,7 +109,7 @@ protected function processRecordForImportUsed(Model $record, $fs, ?string $dir,
104109
105110 $ filename = $ this ->generateImportFileName ($ record );
106111
107- if ($ record instanceof Template && is_array ($ filename )) {
112+ if ($ record instanceof Template && is_array (value: $ filename )) {
108113
109114 foreach ($ filename as $ theme => $ templateFilePath ) {
110115
@@ -115,6 +120,52 @@ protected function processRecordForImportUsed(Model $record, $fs, ?string $dir,
115120 $ fs ->put ($ path , $ templateContent );
116121 }
117122
123+ } elseif ($ record instanceof MediaAsset) {
124+
125+ // Export the JSON metadata file
126+ $ content = $ this ->prepareImportContentFromModel ($ record );
127+ $ path = $ dir . '/ ' . trim ($ filename , '/ ' );
128+ $ fs ->put ($ path , $ content );
129+
130+ // Handle media files
131+ try {
132+ $ dto = ImportEntitiesMediaAsset::fromArray (json_decode ($ content , true ));
133+ foreach ($ dto ->media_files as $ item ) {
134+
135+ $ paths = $ item ['__exported_file_path ' ];
136+
137+ if (!is_array ($ paths ) || empty ($ paths )) {
138+ continue ;
139+ }
140+
141+ foreach ($ paths as $ key => $ value ) {
142+ try {
143+
144+ $ tmpMediaFilePath = collect ([$ dir , $ value ])->map (fn ($ path ) => trim ($ path , '/ ' ))->implode ('/ ' );
145+
146+ // Ensure the directory exists
147+ if (! $ fs ->exists (dirname ($ tmpMediaFilePath ))) {
148+
149+ $ fs ->makeDirectory (dirname ($ tmpMediaFilePath ), 0777 , true );
150+ }
151+
152+ $ mediaFileContent = ($ key == '__real__ ' ? Storage::disk ($ record ->disk ) : Storage::disk ($ record ->conversions_disk ))->get ($ value );
153+
154+ $ fs ->put ($ tmpMediaFilePath , $ mediaFileContent );
155+
156+ } catch (\Throwable $ th ) {
157+ // Skip error, handle next file
158+ }
159+ }
160+ }
161+ } catch (\Throwable $ th ) {
162+ $ errors [] = [
163+ 'record ' => $ record ->getKey (),
164+ 'model ' => get_class ($ record ),
165+ 'message ' => $ th ->getMessage (),
166+ ];
167+ }
168+
118169 } elseif (! is_string ($ filename )) {
119170 $ errors [] = [
120171 'record ' => $ record ->getKey (),
@@ -173,6 +224,12 @@ protected function buildExportingQueryForImportUsed($query, $type)
173224
174225 case ImportDataHelper::FOLDER_IDENTIFIER_TEMPLATE :
175226 return $ query ;
227+
228+ case ImportDataHelper::FOLDER_IDENTIFIER_MEDIAASSET :
229+ return $ query ->with ([
230+ // 'parent',
231+ 'nestableTree ' ,
232+ ]);
176233 }
177234
178235 return $ query ;
0 commit comments