public function test_it_prunes_non_recursively() { $artisan = $this->getArtisan(); $media1 = factory(Media::class)->create(['id' => 1, 'disk' => 'tmp', 'directory' => '']); $media2 = factory(Media::class)->create(['id' => 2, 'disk' => 'tmp', 'directory' => 'foo']); $artisan->call('media:prune', ['disk' => 'tmp', '--non-recursive' => true]); $this->assertEquals([2], Media::pluck('id')->toArray()); $this->assertEquals("Pruned 1 record(s).\n", $artisan->output()); }
/** * Move the file to a new location on disk. * * Will invoke the `save()` method on the model after the associated file has been moved to prevent synchronization errors * @param \Plank\Mediable\Media $media * @param string $directory directory relative to disk root * @param string $name filename. Do not include extension * @return void * @throws \Plank\Mediable\Exceptions\MediaMoveException If attempting to change the file extension or a file with the same name already exists at the destination */ public function move(Media $media, $directory, $filename = null) { $storage = $this->filesystem->disk($media->disk); if ($filename) { $filename = $this->removeExtensionFromFilename($filename, $media->extension); } else { $filename = $media->filename; } $directory = trim($directory, '/'); $target_path = $directory . '/' . $filename . '.' . $media->extension; if ($storage->has($target_path)) { throw MediaMoveException::destinationExists($target_path); } $storage->move($media->getDiskPath(), $target_path); $media->filename = $filename; $media->directory = $directory; $media->save(); }
public function test_it_updates_existing_media() { $this->markTestIncomplete('working locally, sporadically failing in Travis. Need to investigate further.'); $artisan = $this->getArtisan(); $media1 = factory(Media::class)->create(['disk' => 'tmp', 'filename' => 'bar', 'extension' => 'png', 'mime_type' => 'image/png', 'aggregate_type' => 'foo']); $media2 = factory(Media::class)->create(['disk' => 'tmp', 'filename' => 'bar', 'extension' => 'png', 'size' => 8444, 'mime_type' => 'image/png', 'aggregate_type' => 'image']); $this->seedFileForMedia($media1, fopen(__DIR__ . '/../../_data/plank.png', 'r')); $this->seedFileForMedia($media2, fopen(__DIR__ . '/../../_data/plank.png', 'r')); $artisan->call('media:import', ['disk' => 'tmp', '--force' => true]); $this->assertEquals(['image', 'image'], Media::pluck('aggregate_type')->toArray()); $this->assertEquals("Imported 0 file(s).\nUpdated 1 record(s).\nSkipped 1 file(s).\n", $artisan->output()); }
/** * Execute the console command. * * @return void */ public function handle() { $disk = $this->argument('disk'); $directory = $this->option('directory') ?: ''; $recursive = !$this->option('non-recursive'); $counter = 0; $records = Media::inDirectory($disk, $directory, $recursive)->get(); foreach ($records as $media) { if (!$media->fileExists()) { $media->delete(); ++$counter; $this->info("Pruned record for file {$media->getDiskPath()}", 'v'); } } $this->info("Pruned {$counter} record(s)."); }
/** * Execute the console command. * * @return void */ public function handle() { $this->resetCounters(); $disk = $this->argument('disk'); $directory = $this->option('directory') ?: ''; $recursive = !$this->option('non-recursive'); $force = (bool) $this->option('force'); $files = $this->listFiles($disk, $directory, $recursive); $existing_media = Media::inDirectory($disk, $directory, $recursive)->get(); foreach ($files as $path) { if ($record = $this->getRecordForFile($path, $existing_media)) { if ($force) { $this->updateRecordForFile($record, $path); } } else { $this->createRecordForFile($disk, $path); } } $this->outputCounters($force); }
protected function seedFileForMedia(Media $media, $contents = '') { app('filesystem')->disk($media->disk)->put($media->getDiskPath(), $contents); }
public function test_it_can_replace_duplicate_files() { $uploader = $this->mockDuplicateUploader(); $uploader->setOnDuplicateBehavior(MediaUploader::ON_DUPLICATE_REPLACE); $method = $this->getPrivateMethod($uploader, 'verifyDestination'); $media = factory(Media::class)->create(['disk' => 'tmp', 'directory' => '', 'filename' => 'plank', 'extension' => 'png']); $method->invoke($uploader, $media); $this->assertEquals(0, Media::all()->count()); }
/** * Delete the media that previously existed at a destination. * @param \Plank\Mediable\Media $model * @return void */ private function deleteExistingMedia(Media $model) { Media::where('disk', $model->disk)->where('directory', $model->directory)->where('filename', $model->filename)->where('extension', $model->extension)->first()->delete(); }
public function test_it_can_be_queried_by_path_on_disk_when_directory_is_empty() { factory(Media::class)->create(['id' => 4, 'disk' => 'tmp', 'directory' => '', 'filename' => 'bat', 'extension' => 'jpg']); $this->assertEquals(4, Media::forPathOnDisk('tmp', 'bat.jpg')->first()->id); }
/** * Get a list of all tags that the media is attached to. * @param \Plank\Mediable\Media $media * @return array */ public function getTagsForMedia(Media $media) { $this->rehydrateMediaIfNecessary(); return $this->media->reduce(function ($carry, Media $item) use($media) { if ($item->getKey() === $media->getKey()) { $carry[] = $item->pivot->tag; } return $carry; }, []); }