Skip to content

Commit

Permalink
return image content (#582)
Browse files Browse the repository at this point in the history
* return image content

* fix test
  • Loading branch information
nadar authored Oct 15, 2020
1 parent 142900b commit 6e274d5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/apis/ProxyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public function actionFileProvider($machine, $buildToken, $fileId)
$file = Yii::$app->storage->getFile($fileId);
/* @var $file \luya\admin\file\Item */
if ($file && $file->fileExists) {
return Yii::$app->response->sendContentAsFile($file->getContent(), $file->serverSource, null, ['mimeType' => $file->mimeType])->send();
return Yii::$app->response->sendContentAsFile($file->getContent(), $file->systemFileName, null, ['mimeType' => $file->mimeType])->send();
}

throw new NotFoundHttpException("The requested file '".$fileId."' does not exist in the storage folder.");
Expand Down Expand Up @@ -237,7 +237,7 @@ public function actionImageProvider($machine, $buildToken, $imageId)
$image = Yii::$app->storage->getImage($imageId);
/* @var $image \luya\admin\image\Item */
if ($image && $image->fileExists) {
return Yii::$app->response->sendFile($image->serverSource)->send();
return Yii::$app->response->sendContentAsFile($image->getContent(), $image->systemFileName)->send();
}

throw new NotFoundHttpException("The requested image '".$imageId."' does not exist in the storage folder.");
Expand Down
13 changes: 13 additions & 0 deletions src/image/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* @property integer $resolutionHeight Get the image resolution height.
* @property \luya\admin\file\Item $file The file object where the image was created from.
* @property string $systemFileName the system file name.
* @property string $content The the image file content.
*
* @author Basil Suter <basil@nadar.io>
* @since 1.0.0
Expand Down Expand Up @@ -148,6 +149,7 @@ public function getFileExists()
}

/**
* Get the filename on the filesystem.
*
* @return string
* @since 1.2.0
Expand All @@ -157,6 +159,17 @@ public function getSystemFileName()
return $this->getFilterId() . '_' . $this->getFile()->getSystemFileName();
}

/**
* Get the content of the image
*
* @return string
* @since 3.7.0
*/
public function getContent()
{
return Yii::$app->storage->fileSystemContent($this->systemFileName);
}

/**
* Get the image resolution width in Pixel.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/admin/apis/ProxyControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function testActionImageProviderMissingImage()
public function testActionImageProvide()
{
$ctrl = new ProxyController('proxy', $this->app->getModule('admin'));
$this->expectException(ErrorException::class);
$this->expectException(HeadersAlreadySentException::class);
$ctrl->actionImageProvider('machine', 'token', 1);
}

Expand Down

0 comments on commit 6e274d5

Please sign in to comment.