/** * Execute the job. * * @param FileManager $fileRepository */ public function handle(FileManager $fileRepository) { $this->logger = $this->getJobLogger(); $this->logger->debug("Deleting file {$this->path} attempt {$this->attempts()}"); $fileRepository->deleteFile($this->path); $this->logger->debug("Deleting remote file {$this->path} attempt {$this->attempts()}"); $fileRepository->deleteRemoteFile($this->path); }
/** * Execute the job. * * @param FileManager $fileRepository * @param CacheManager $cacheManager * * @throws Exception */ public function handle(FileManager $fileRepository, CacheManager $cacheManager) { $this->logger = $this->getJobLogger(); $this->logger->debug("Optimizing file {$this->imageFile->getId()} {$this->imageFile->getPath()} attempt {$this->attempts()}", $this->imageFile->toArray()); // Get a fresh copy from the DB (checks if it's deleted) if (!($this->imageFile = $this->imageFile->fresh())) { throw new Exception("ImageFile no longer exists."); } $this->optimize(); $fileRepository->copyToRemote($this->imageFile); $cacheManager->purge($this->imageFile->getPath()); }
/** * Execute the job. * * @param FileManager $fileManager * * @throws Exception */ public function handle(FileManager $fileManager) { $this->logger = $this->getJobLogger(); $this->logger->debug("Generating thumbnail for album {$this->album->getId()}" . " attempt {$this->attempts()}", $this->album->toArray()); // Get a fresh copy from the DB (checks if it's deleted) if (!($this->album = $this->album->fresh())) { throw new Exception("Album no longer exists."); } // Get first 4 album pictures /** @var Image[] $images */ $images = $this->album->images()->orderBy('imageId', 'DESC')->take(4)->get(); $filenames = []; foreach ($images as $image) { $imageFile = $image->getImageFile(); try { // This will make sure the file exists locally $picture = $fileManager->getPictureForImageFile($imageFile); } catch (Exception $e) { echo "Unable to download image file {$imageFile->getPath()} " . $e->getMessage() . PHP_EOL; continue; } if (@file_exists($imageFile->getAbsolutePath())) { // The [0] at the end is to cater for animated gifs // - use the first frame $filenames[] = '"' . $imageFile->getAbsolutePath() . '[0]"'; } else { echo "Unable to download image file {$imageFile->getPath()}" . PHP_EOL; } } if (empty($filenames)) { return false; } print_r($filenames); $thumbFilename = $fileManager->prepareLocalFile('albumthumb', 'jpg'); $thumbAbsolutePath = Config::get('app.data_dir') . 'albumthumb/' . $thumbFilename; $cmd = "montage " . implode(' ', $filenames) . " -gravity center -resize \"50^\" -crop 50x50+0+0 -geometry 50x50 -tile 2x2 " . $thumbAbsolutePath; echo $cmd . PHP_EOL; passthru($cmd); if ($fileSize = filesize($thumbAbsolutePath)) { $imageFile = new ImageFile(['directory' => 'albumthumb', 'filename' => $thumbFilename, 'width' => 100, 'height' => 100, 'size' => $fileSize]); $imageFile->save(); $this->dispatch(new OptimizeFileJob($imageFile)); $this->album->thumbnailImageFileId = $imageFile->imageFileId; $this->album->save(); } else { throw new Exception("Unable to create montage"); } }
/** * Execute the job. * * @param FileManager $fileManager * * @throws Exception */ public function handle(FileManager $fileManager) { $this->logger = $this->getJobLogger(); $this->logger->debug("Generating thumbnail for image {$this->imageFile->getId()} {$this->imageFile->getPath()}" . " attempt {$this->attempts()}", $this->imageFile->toArray()); // Get a fresh copy from the DB (checks if it's deleted) if (!($this->imageFile = $this->imageFile->fresh())) { throw new Exception("ImageFile no longer exists."); } // Get full size image $picture = $fileManager->getPictureForImageFile($this->imageFile); $filename = $this->imageFile->getFilename(); // Generate 200x200 thumbnail $picture->fit(200, 200, function (Constraint $constraint) { $constraint->upsize(); }); if ($thumbnailImageFile = $fileManager->savePicture($picture, 'thumb', $filename, $this->imageFile)) { Image::where('imageFileID', $this->imageFile->getId())->update(['thumbnailImageFileId' => $thumbnailImageFile->getId()]); } }
public function testNginxCachePurge() { $image = $this->getImage(); $filename = 'tests/' . $this->fileRepository->savePicture($image, 'tests'); $path = $this->dataDir . $filename; $s3Url = $this->s3Url . $filename; $cloudflareUrl = $this->cloudflareDataUrl . $filename; $directUrl = $this->directDataUrl . $filename; // Test that it is first served by nginx from the local filesystem $headers = $this->getHttpHeaders($directUrl); $this->assertNotContains('x-amz-request-id', $headers); $this->assertNotContains('X-Cache-Status', $headers); // Delete from local filesystem unlink($path); // Test that it is served from AWS via Nginx $headers = $this->getHttpHeaders($directUrl); $this->assertContains('x-amz-request-id', $headers); $this->assertContains('X-Cache-Status: MISS', $headers); sleep(1); // Test that it is cached by Nginx now $headers = $this->getHttpHeaders($directUrl); $this->assertContains('x-amz-request-id', $headers); $this->assertContains('X-Cache-Status: HIT', $headers); // Delete from remote $this->fileRepository->deleteRemoteFile($filename); $this->assertNotEquals(200, $this->getHttpResponseCode($s3Url), $s3Url); // Test that it is still cached by Nginx $headers = $this->getHttpHeaders($directUrl); $this->assertContains('HTTP/1.1 200', $headers); $this->assertContains('X-Cache-Status: HIT', $headers); // Purge nginx cache $this->assertEquals(true, $this->cacheManager->purgeNginx($directUrl)); // Test it's no longer being served $this->assertNotEquals(200, $this->getHttpResponseCode($directUrl), $directUrl); $this->fileRepository->deleteFile($filename); }
/** * If this image doesn't already have an original image file backed up, * move the current image file to be the backup. * * @param FileManager $fileManager * * @return bool */ public function moveOriginalFile(FileManager $fileManager) { if ($this->hasOriginal()) { return true; } // Backup uncropped image $originalImageFile = $this->getImageFile(); $fileManager->moveFile($originalImageFile, FileManager::UNCROPPED_DIR); $this->setUncroppedImageFile($originalImageFile); return true; }
/** * @api {put} /images/{imageId}/image?action=rotate Rotate an Image * @apiGroup Manipulating Images * @apiDescription Rotate an image clockwise or counter-clockwise. * @apiParam {string=rotate} action Action to perform. * @apiParam {int=90,180,270} degrees Degrees to rotate by. * @apiParam {string=cw,ccw} [direction=cw] Direction to rotate in (clockwise or counter-clockwise respectively). * @apiUse RequiresEditableImage * @apiUse ImageSuccessResponse * * @param Image $image * @param FileManager $fileManager * * @throws Exception * @return Response */ protected function rotate(Image $image, FileManager $fileManager) { $success = false; $this->validate($this->request, ['degrees' => 'required|integer|in:90,180,270', 'direction' => 'in:cw,ccw']); if ($image->operationInProgress) { throw new HttpException(409, "Another operation is currently in progress for this image."); } $image->operationInProgress = true; $image->save(); $degrees = (int) $this->request->input('degrees'); $direction = $this->request->has('direction') ? $this->request->input('direction') : 'cw'; if ($direction === 'cw') { $degrees = -$degrees; } $picture = $fileManager->getPictureForImageFile($image->imageFile); $picture->rotate($degrees); if ($newImageFile = $fileManager->savePicture($picture)) { $image->setImageFile($newImageFile); $image->operationInProgress = false; $success = $image->save(); } return $this->response(['success' => $success, 'image' => $image->fresh()]); }
/** * Execute the job. * * @param FileManager $fileRepository */ public function handle(FileManager $fileRepository) { $this->logger = $this->getJobLogger(); $this->logger->debug("Moving remote file {$this->oldPath} to {$this->newPath} attempt {$this->attempts()}"); $fileRepository->moveRemoteFile($this->oldPath, $this->newPath); }