- /**
- * Update the content of an existing image.
- * Uploaded the new image content and creates a revision for the old image content.
- * @param Image $image
- * @param $imageData
- * @return Image
- * @throws ImageUploadException
- */
- private function update(Image $image, $imageData)
- {
- // Save image revision if not previously exists to ensure we always have
- // a reference to the image files being uploaded.
- if ($image->revisions()->count() === 0) {
- $this->saveImageRevision($image);
- }
-
- $pathInfo = pathinfo($image->path);
- $revisionCount = $image->revisionCount() + 1;
- $newFileName = preg_replace('/^(.+?)(-v\d+)?$/', '$1-v' . $revisionCount, $pathInfo['filename']);
-
- $image->path = str_replace_last($pathInfo['filename'], $newFileName, $image->path);
- $image->url = $this->getPublicUrl($image->path);
- $image->updated_by = user()->id;
-
- $storage = $this->getStorage();
-
- try {
- $storage->put($image->path, $imageData);
- $storage->setVisibility($image->path, 'public');
- $image->save();
- $this->saveImageRevision($image);
- } catch (Exception $e) {
- throw new ImageUploadException(trans('errors.path_not_writable', ['filePath' => $image->path]));
- }
- return $image;
- }
-
- /**
- * Save a new revision for an image.
- * @param Image $image
- * @return ImageRevision
- */
- protected function saveImageRevision(Image $image)
- {
- $revision = new ImageRevision();
- $revision->image_id = $image->id;
- $revision->path = $image->path;
- $revision->url = $image->url;
- $revision->created_by = user()->id;
- $revision->revision = $image->revisionCount() + 1;
- $revision->save();
- return $revision;
- }