Skip to content

Commit

Permalink
Refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean committed Dec 18, 2024
1 parent 0f30dc6 commit 389d569
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions src/Transformers/AssetsTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,9 @@ public function transform(string $value): null|string|array
->container($assetContainer)
->path($this->assetPath($assetContainer, $path));

if ($this->config('process_downloaded_images')) {
Storage::disk('local')->put($tempPath = 'statamic/temp-assets/'.uniqid().'.'.Str::afterLast($path, '.'), $request->body());

$uploadedFile = new UploadedFile(Storage::disk('local')->path($tempPath), $asset->basename(), Storage::mimeType($tempPath));

$source = $this->processSourceFile($uploadedFile);

$assetContainer->disk()->put($asset->path(), $stream = fopen($source, 'r'));

if (is_resource($stream)) {
fclose($stream);
}

app('files')->delete($source);
Storage::disk('local')->delete($tempPath);
} else {
$assetContainer->disk()->put($asset->path(), $request->body());
}
$this->config('process_downloaded_images')
? $this->processAssetUsingSourcePreset($asset, $path, $request->body())
: $assetContainer->disk()->put($asset->path(), $request->body());

$asset->save();
}
Expand Down Expand Up @@ -112,11 +97,35 @@ private function assetPath(AssetContainerContract $assetContainer, string $path)
return $path;
}

protected function preset()
private function processAssetUsingSourcePreset($asset, string $path, string $contents): void
{
return AssetContainer::find($this->field->get('container'))->sourcePreset();
Storage::disk('local')->put($tempPath = 'statamic/temp-assets/'.uniqid().'.'.Str::afterLast($path, '.'), $contents);

$uploadedFile = new UploadedFile(
path: Storage::disk('local')->path($tempPath),
originalName: $asset->basename(),
mimeType: Storage::mimeType($tempPath)
);

$source = $this->processSourceFile($uploadedFile);

$asset->container()->disk()->put($asset->path(), $stream = fopen($source, 'r'));

if (is_resource($stream)) {
fclose($stream);
}

app('files')->delete($source);
Storage::disk('local')->delete($tempPath);
}

/**
* This method has been copied from Core's Uploader class. We don't need the rest of the
* Uploader, just this method so copying it was the easiest solution.
*
* @param UploadedFile $file
* @return string
*/
private function processSourceFile(UploadedFile $file): string
{
if ($file->getMimeType() === 'image/gif') {
Expand Down Expand Up @@ -144,6 +153,11 @@ private function processSourceFile(UploadedFile $file): string
}
}

private function preset()
{
return AssetContainer::find($this->field->get('container'))->sourcePreset();
}

public function fieldItems(): array
{
$assetContainer = AssetContainer::find($this->field->get('container'));
Expand Down

0 comments on commit 389d569

Please sign in to comment.