mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-04-23 04:10:22 +00:00
Image update API: added update image file ability
This commit is contained in:
parent
f78c0635ee
commit
cd4b612019
3 changed files with 25 additions and 2 deletions
|
@ -30,6 +30,7 @@ class ImageGalleryApiController extends ApiController
|
||||||
],
|
],
|
||||||
'update' => [
|
'update' => [
|
||||||
'name' => ['string', 'max:180'],
|
'name' => ['string', 'max:180'],
|
||||||
|
'image' => ['file', ...$this->getImageValidationRules()],
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -89,7 +90,8 @@ class ImageGalleryApiController extends ApiController
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the details of an existing image in the system.
|
* Update the details of an existing image in the system.
|
||||||
* Only allows updating of the image name at this time.
|
* Since "image" is expected to be a file, this needs to be a 'multipart/form-data' type request if providing a
|
||||||
|
* new image file. Updated image files should be of the same file type as the original image.
|
||||||
*/
|
*/
|
||||||
public function update(Request $request, string $id)
|
public function update(Request $request, string $id)
|
||||||
{
|
{
|
||||||
|
@ -99,6 +101,9 @@ class ImageGalleryApiController extends ApiController
|
||||||
$this->checkOwnablePermission('image-update', $image);
|
$this->checkOwnablePermission('image-update', $image);
|
||||||
|
|
||||||
$this->imageRepo->updateImageDetails($image, $data);
|
$this->imageRepo->updateImageDetails($image, $data);
|
||||||
|
if (isset($data['image'])) {
|
||||||
|
$this->imageRepo->updateImageFile($image, $data['image']);
|
||||||
|
}
|
||||||
|
|
||||||
return response()->json($this->formatForSingleResponse($image));
|
return response()->json($this->formatForSingleResponse($image));
|
||||||
}
|
}
|
||||||
|
|
|
@ -175,6 +175,7 @@ class ImageRepo
|
||||||
throw new ImageUploadException(trans('errors.image_upload_replace_type'));
|
throw new ImageUploadException(trans('errors.image_upload_replace_type'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$image->refresh();
|
||||||
$image->updated_by = user()->id;
|
$image->updated_by = user()->id;
|
||||||
$image->save();
|
$image->save();
|
||||||
$this->imageService->replaceExistingFromUpload($image->path, $image->type, $file);
|
$this->imageService->replaceExistingFromUpload($image->path, $image->type, $file);
|
||||||
|
|
|
@ -295,7 +295,24 @@ class ImageGalleryApiTest extends TestCase
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_update_endpoint_requires_image_delete_permission()
|
public function test_update_existing_image_file()
|
||||||
|
{
|
||||||
|
$this->actingAsApiAdmin();
|
||||||
|
$imagePage = $this->entities->page();
|
||||||
|
$data = $this->files->uploadGalleryImageToPage($this, $imagePage);
|
||||||
|
$image = Image::findOrFail($data['response']->id);
|
||||||
|
|
||||||
|
$this->assertFileEquals($this->files->testFilePath('test-image.png'), public_path($data['path']));
|
||||||
|
|
||||||
|
$resp = $this->call('PUT', $this->baseEndpoint . "/{$image->id}", [], [], [
|
||||||
|
'image' => $this->files->uploadedImage('my-cool-image.png', 'compressed.png'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$resp->assertStatus(200);
|
||||||
|
$this->assertFileEquals($this->files->testFilePath('compressed.png'), public_path($data['path']));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_update_endpoint_requires_image_update_permission()
|
||||||
{
|
{
|
||||||
$user = $this->users->editor();
|
$user = $this->users->editor();
|
||||||
$this->actingAsForApi($user);
|
$this->actingAsForApi($user);
|
||||||
|
|
Loading…
Add table
Reference in a new issue