/** * @param $fileId * @param Temp $temp * @return \Symfony\Component\HttpFoundation\BinaryFileResponse */ public function get($fileId, Temp $temp) { /** @var Temp $fileModel */ $fileModel = $temp->findOrFail($fileId); try { $file = $fileModel->getFile(); } catch (FileNotFoundException $exc) { abort(404); } return response()->download($file); }
/** * Run command. */ public function handle() { $deleteBefore = Carbon::now()->subWeek(); $filesToDelete = $this->tempModel->where('created_at', '<=', $deleteBefore)->get(); $failed = []; foreach ($filesToDelete as $file) { // delete the file if (!\File::delete($file->getTempPath())) { $this->output->warning(sprintf('%s file not found. Skipping...', $file->getTempPath())); } } $deletedRecords = DB::table($this->tempModel->getTable())->where('created_at', '<=', $deleteBefore)->delete(); $this->output->success(sprintf('%d temp file cleaned', $deletedRecords)); }
/** * @param $id * @param Temp $tempModel * @return \Symfony\Component\HttpFoundation\BinaryFileResponse */ public function preview($id, Temp $tempModel) { try { /** @var Temp $tempImage */ $tempImage = $tempModel->findOrFail($id); } catch (ModelNotFoundException $exc) { abort(404); } $image = \Image::make($tempImage->getTempPath()); $width = config('ignicms.images.admin_thumb_width'); $height = config('ignicms.images.admin_thumb_height'); $image->fit($width, $height); return $image->response(); }
/** * @param Temp|UploadedFile|File $file * @param array $options * @return array * @throws \Exception */ public function manipulateImage($file, array $options) { // Detect file type if ($file instanceof Temp) { $sanitizedFilename = $this->sanitizeFilename($file->filename); } elseif ($file instanceof UploadedFile) { $sanitizedFilename = $this->sanitizeFilename($file->getClientOriginalName()); } elseif ($file instanceof File) { $actualFilename = str_replace('_source.', '.', $file->getFilename()); $sanitizedFilename = $this->sanitizeFilename($actualFilename); $sourceFile = clone $file; } else { throw new \Exception('Unexpected file of class ' . get_class($file)); } $images = []; $pathParts = pathinfo($sanitizedFilename); // Move uploaded file and rename it as source file if this is needed. // We need to generate unique name if the name is already in use. if (!isset($sourceFile)) { $filename = $pathParts['filename'] . '_source.' . $pathParts['extension']; $sourceFile = $file->move($this->getThumbnailPath(), $filename); } $images['original']['source'] = $sourceFile; // If we have retina factor if ($this->getRetinaFactor()) { // Generate retina image by just copying the source. $retinaFilename = $this->generateRetinaName($sanitizedFilename); FileFacade::copy($sourceFile->getRealPath(), $this->getThumbnailPath() . $retinaFilename); $images['original']['retina'] = Image::make($this->getThumbnailPath() . $retinaFilename); // The original image is scaled down version of the source. $originalImage = Image::make($sourceFile->getRealPath()); $width = round($originalImage->getWidth() / $this->getRetinaFactor()); $height = round($originalImage->getHeight() / $this->getRetinaFactor()); $originalImage->resize($width, $height); $images['original']['original_file'] = $originalImage->save($this->getThumbnailPath() . $sanitizedFilename); // Generate thumbs foreach ($options['thumbnails'] as $thumbnailName => $thumbnailOptions) { // Create retina thumb $images['thumbnails'][$thumbnailName]['retina'] = $this->createThumbnail($sourceFile->getRealPath(), $thumbnailName, $this->generateRetinaName($sanitizedFilename), $thumbnailOptions['width'] * $this->getRetinaFactor(), $thumbnailOptions['height'] * $this->getRetinaFactor(), $thumbnailOptions['type'], array_get($thumbnailOptions, 'color')); // Create original thumb $images['thumbnails'][$thumbnailName]['original'] = $this->createThumbnail($sourceFile->getRealPath(), $thumbnailName, $sanitizedFilename, $thumbnailOptions['width'], $thumbnailOptions['height'], $thumbnailOptions['type'], array_get($thumbnailOptions, 'color')); } } else { // Copy source file. $filename = $this->sanitizeFilename($file->getFilename()); FileFacade::copy($sourceFile->getRealPath(), $this->getThumbnailPath() . $filename); $images['original']['original_file'] = Image::make($this->getThumbnailPath() . $filename); // Generate thumbs foreach ($options['thumbnails'] as $thumbnailName => $thumbnailOptions) { // Create original thumb $images['thumbnails'][$thumbnailName]['original'] = $this->createThumbnail($sourceFile->getRealPath(), $thumbnailName, $sanitizedFilename, $thumbnailOptions['width'], $thumbnailOptions['height'], $thumbnailOptions['type']); } } return $images; }
* `alt` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, * `title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, * `meta` text COLLATE utf8_unicode_ci, */ return ['resource_id' => 0, 'resource_model' => '\\Test\\Class', 'image_type' => 'test', 'original_image' => $faker->image('/tmp', 640, 480, 'cats', false), 'retina_factor' => rand(1, 4), 'order' => 0, 'alt' => 'Alt', 'title' => 'title', 'meta' => ['testing' => 1]]; }); $factory->define(\Despark\Cms\Models\File\Temp::class, function (Faker\Generator $faker) { /* * `id` int(10) unsigned NOT NULL AUTO_INCREMENT, * `filename` varchar(255) COLLATE utf8_unicode_ci NOT NULL, * `temp_filename` varchar(255) COLLATE utf8_unicode_ci NOT NULL, * `file_type` varchar(255) COLLATE utf8_unicode_ci NOT NULL, * `created_at` timestamp NULL DEFAULT NULL, * `updated_at` timestamp NULL DEFAULT NULL, */ $image = $faker->image(\Despark\Cms\Models\File\Temp::getTempDirectory(), 10, 10, 'cats', false); return ['filename' => $faker->name, 'temp_filename' => $image, 'file_type' => $faker->mimeType]; }); $factory->define(\Despark\Cms\Models\Video::class, function (Faker\Generator $faker) { /* * `resource_id` int(10) unsigned NOT NULL, * `resource_model` varchar(128) COLLATE utf8_unicode_ci NOT NULL, * `field` varchar(255) COLLATE utf8_unicode_ci NOT NULL, * `provider` varchar(32) COLLATE utf8_unicode_ci NOT NULL, * `video_id` varchar(128) COLLATE utf8_unicode_ci NOT NULL, * `config` json NOT NULL, */ return ['resource_id' => 0, 'resource_model' => 0, 'field' => 'field', 'provider' => 'youtube', 'video_id' => 'C0DPdy98e4c', 'config' => ['width' => 300, 'height' => 150]]; }); $factory->define(\Despark\Tests\Cms\Resources\TestResourceModel::class, function (Faker\Generator $faker) { return ['test_field' => $faker->word];