/** * Update the details of an image via an array of properties. * @param Image $image * @param array $updateDetails * @return Image */ public function updateImageDetails(Image $image, $updateDetails) { $image->fill($updateDetails); $image->save(); $this->loadThumbs($image); return $image; }
/** * Run the migrations. * * @return void */ public function up() { Schema::table('images', function (Blueprint $table) { $table->string('path', 400); $table->string('type')->index(); }); Image::all()->each(function ($image) { $image->path = $image->url; $image->type = 'gallery'; $image->save(); }); }
public function test_image_delete() { $page = \BookStack\Page::first(); $this->asAdmin(); $imageName = 'first-image.jpg'; $relPath = $this->uploadImage($imageName, $page->id); $image = \BookStack\Image::first(); $this->call('DELETE', '/images/' . $image->id); $this->assertResponseOk(); $this->dontSeeInDatabase('images', ['url' => $this->baseUrl . $relPath, 'type' => 'gallery']); $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded image has been deleted'); }
/** * Saves a new image * @param string $imageName * @param string $imageData * @param string $type * @param int $uploadedTo * @return Image * @throws ImageUploadException */ private function saveNew($imageName, $imageData, $type, $uploadedTo = 0) { $storage = $this->getStorage(); $secureUploads = setting('app-secure-images'); $imageName = str_replace(' ', '-', $imageName); if ($secureUploads) { $imageName = str_random(16) . '-' . $imageName; } $imagePath = '/uploads/images/' . $type . '/' . Date('Y-m-M') . '/'; if ($this->isLocal()) { $imagePath = '/public' . $imagePath; } while ($storage->exists($imagePath . $imageName)) { $imageName = str_random(3) . $imageName; } $fullPath = $imagePath . $imageName; try { $storage->put($fullPath, $imageData); $storage->setVisibility($fullPath, 'public'); } catch (Exception $e) { throw new ImageUploadException('Image Path ' . $fullPath . ' is not writable by the server.'); } if ($this->isLocal()) { $fullPath = str_replace_first('/public', '', $fullPath); } $imageDetails = ['name' => $imageName, 'path' => $fullPath, 'url' => $this->getPublicUrl($fullPath), 'type' => $type, 'uploaded_to' => $uploadedTo]; if (user()->id !== 0) { $userId = user()->id; $imageDetails['created_by'] = $userId; $imageDetails['updated_by'] = $userId; } $image = Image::forceCreate($imageDetails); return $image; }
/** * Destroys an Image object along with its files and thumbnails. * @param Image $image * @return bool */ public function destroyImage(Image $image) { $storage = $this->getStorage(); $imageFolder = dirname($image->path); $imageFileName = basename($image->path); $allImages = collect($storage->allFiles($imageFolder)); $imagesToDelete = $allImages->filter(function ($imagePath) use($imageFileName) { $expectedIndex = strlen($imagePath) - strlen($imageFileName); return strpos($imagePath, $imageFileName) === $expectedIndex; }); $storage->delete($imagesToDelete->all()); // Cleanup of empty folders foreach ($storage->directories($imageFolder) as $directory) { if ($this->isFolderEmpty($directory)) { $storage->deleteDirectory($directory); } } if ($this->isFolderEmpty($imageFolder)) { $storage->deleteDirectory($imageFolder); } $image->delete(); return true; }