Skip to content

Commit

Permalink
Clean up uploaded media filenames, media w/h from bytes not data
Browse files Browse the repository at this point in the history
  • Loading branch information
cbutcosk committed Jul 12, 2024
1 parent 7411b49 commit dc0f9cc
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions app/Console/Commands/MigrateOSCIPublicationOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use App\Models\Vendor\Block;
use A17\Twill\Models\Media;


class MigrateOSCIPublicationOne extends Command
{
/**
Expand All @@ -29,34 +30,49 @@ class MigrateOSCIPublicationOne extends Command
*/
protected $description = 'Migrate an OSCI publication from a migration file to a website DigitalPublication';

/**
* mediaFactory
*
* Fetches asset for this figure layer and registers it as Media,
* applying caption and using OSCI's fallback URL
*
* @param imageData - JSON of image data for this layer
* @param caption_html - HTML of caption data
* @param fallback_url - Fallback image to use for this layer
*
*/
private function mediaFactory($imageData, $caption_html, $fallback_url)
{

// Media uploads and relations
$imageUrl = isset($imageData->static_url) ? $imageData->static_url : $fallback_url ;

// TODO: Load JPEG-converted PTIFF URL from this from db's `assets` table
// TODO: Load 360-zip URL from this from db's `assets` table

// Construct the UUID for this asset and preserve filename info
$imageUuid = (string) Str::uuid();
$imageUrlPath = parse_url($imageUrl, PHP_URL_PATH);
$imageExt = pathinfo($imageUrlPath, PATHINFO_EXTENSION);
$imageFilename = Str::random(10) . $imageExt;
$imageFilename = pathinfo($imageUrlPath, PATHINFO_FILENAME);
$imageFilename = Str::random(10) . '-' . sanitizeFilename($imageFilename);

$imageName = $imageUuid . '/' . $imageFilename;

// TODO: Catch error and retry-while
$ctx = stream_context_create(array(
'http' =>
array(
'timeout' => 120,
)
));
$imageContent = file_get_contents($imageUrl, false, $ctx);
[$width, $height] = getimagesizefromstring($imageContent);

Storage::disk('s3')->put($imageName, $imageContent);

// TODO: Use the CMS's onboard h/w helpers
$media = new Media([
'uuid' => $imageName,
'width' => $imageData->width,
'height' => $imageData->height,
'width' => $width,
'height' => $height,
'filename' => $imageFilename,
'locale' => 'en'
]);
Expand All @@ -76,7 +92,6 @@ private function configureFigBlock($figure, $block)

$layers = isset($figure->layers_data) ? json_decode($figure->layers_data) : [];

// TODO: if html_figure and html_content_src, that's a video block bby
switch (true) {
// Non-video embeds (HTML, mostly) become media_embed
case $figure->figure_type === 'html_figure' && !isset($figure->html_content_src):
Expand Down Expand Up @@ -107,7 +122,6 @@ private function configureFigBlock($figure, $block)
break;

// OSCI's layered_image with only one layer should just be an image
// TODO: Move IIP handling to a func and handle iip_asset in this `case`
case 'layered_image' && count($layers) === 1:
$block->type = 'image';
$block->content = [
Expand Down Expand Up @@ -135,8 +149,8 @@ private function configureFigBlock($figure, $block)
'crop' => 'desktop',
'crop_x' => 0,
'crop_y' => 0,
'crop_w' => $imageData->width,
'crop_h' => $imageData->height
'crop_w' => $media->width,
'crop_h' => $media->height
]);

$block->save();
Expand All @@ -163,9 +177,11 @@ private function configureFigBlock($figure, $block)
$layerBlock = new Block();
$layerBlock->blockable_id = $block->blockable_id;
$layerBlock->blockable_type = 'digitalPublicationArticles';
$layerBlock->position = 1; // TODO: order
$layerBlock->position = 1;
$layerBlock->parent_id = $block->id;

// TODO: Use $figure->$options->baseLayerPreset and $figure->$options->annotationPreset to determine the order and kind of this layer

// OSCI doesn't expose overlay vs image data so parse the URL filename
$imageUrl = isset($imageData->static_url) ? $imageData->static_url : $figure->fallback_url ;
$imageUrlPath = parse_url($imageUrl, PHP_URL_PATH);
Expand Down Expand Up @@ -199,8 +215,8 @@ private function configureFigBlock($figure, $block)
'crop' => 'desktop',
'crop_x' => 0,
'crop_y' => 0,
'crop_w' => $imageData->width,
'crop_h' => $imageData->height
'crop_w' => $media->width,
'crop_h' => $media->height
]);
$layerBlock->save();

Expand All @@ -212,7 +228,6 @@ private function configureFigBlock($figure, $block)
break;

case 'iip_asset':
// TODO: Properly handle the IIP asset (finally!)
$block->type = 'image';
$block->content = [
"is_modal" => false,
Expand Down Expand Up @@ -241,8 +256,8 @@ private function configureFigBlock($figure, $block)
'crop' => 'desktop',
'crop_x' => 0,
'crop_y' => 0,
'crop_w' => $imageData->width,
'crop_h' => $imageData->height
'crop_w' => $media->width,
'crop_h' => $media->height
]);

$block->save();
Expand All @@ -255,7 +270,7 @@ private function configureFigBlock($figure, $block)
"caption" => $figure->caption_html
];

// TODO: Attach media
// TODO: use this->mediaFactory to fetch + attach 360 zip
$block->save();
break;

Expand Down

0 comments on commit dc0f9cc

Please sign in to comment.