From f5175cc59dde790a3dd269ce74902f566e5bab57 Mon Sep 17 00:00:00 2001 From: Diogo Peralta Cordeiro Date: Tue, 10 Aug 2021 20:24:11 +0100 Subject: [PATCH] [ATTACHMENTS] Always store in the same location --- components/Avatar/Controller/Avatar.php | 3 +-- components/Posting/Posting.php | 3 +-- src/Core/GSFile.php | 12 +++--------- src/DataFixtures/CoreFixtures.php | 3 +-- 4 files changed, 6 insertions(+), 15 deletions(-) diff --git a/components/Avatar/Controller/Avatar.php b/components/Avatar/Controller/Avatar.php index 3002c50b50..6fef8d170b 100644 --- a/components/Avatar/Controller/Avatar.php +++ b/components/Avatar/Controller/Avatar.php @@ -106,8 +106,7 @@ class Avatar extends Controller throw new ClientException('Invalid form'); } $attachment = GSFile::sanitizeAndStoreFileAsAttachment( - $file, - dest_dir: Common::config('attachments', 'dir') + $file ); // Delete current avatar if there's one $avatar = DB::find('avatar', ['gsactor_id' => $gsactor_id]); diff --git a/components/Posting/Posting.php b/components/Posting/Posting.php index 9597a898ec..ed5cb4bea5 100644 --- a/components/Posting/Posting.php +++ b/components/Posting/Posting.php @@ -122,8 +122,7 @@ END; $filesize = $f->getSize(); Event::handle('EnforceQuota', [$actor_id, $filesize]); $processed_attachments[] = GSFile::sanitizeAndStoreFileAsAttachment( - $f, - dest_dir: Common::config('attachments', 'dir') + $f ); } diff --git a/src/Core/GSFile.php b/src/Core/GSFile.php index 4782a0086a..43e127f871 100644 --- a/src/Core/GSFile.php +++ b/src/Core/GSFile.php @@ -29,7 +29,6 @@ use App\Util\Exception\DuplicateFoundException; use App\Util\Exception\NoSuchFileException; use App\Util\Exception\NotFoundException; use App\Util\Exception\ServerException; -use App\Util\Formatting; use SplFileInfo; use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\HeaderUtils; @@ -62,13 +61,8 @@ class GSFile * * @return Attachment */ - public static function sanitizeAndStoreFileAsAttachment(SplFileInfo $file, - string $dest_dir): Attachment + public static function sanitizeAndStoreFileAsAttachment(SplFileInfo $file): Attachment { - if (!Formatting::startsWith($dest_dir, Common::config('storage', 'dir'))) { - throw new \InvalidArgumentException("Attempted to store a file in a directory outside the GNU social files location: {$dest_dir}"); - } - $hash = null; Event::handle('HashFile', [$file->getPathname(), &$hash]); try { @@ -83,12 +77,12 @@ class GSFile $attachment = Attachment::create([ 'filehash' => $hash, 'mimetype' => $mimetype, - 'filename' => Formatting::removePrefix($dest_dir, Common::config('attachments', 'dir')) . $hash, + 'filename' => $hash, 'size' => $file->getSize(), 'width' => $width, 'height' => $height, ]); - $file->move($dest_dir, $hash); + $file->move(Common::config('attachments', 'dir'), $hash); DB::persist($attachment); Event::handle('AttachmentStoreNew', [&$attachment]); } diff --git a/src/DataFixtures/CoreFixtures.php b/src/DataFixtures/CoreFixtures.php index 2de0dc7a44..e9f7a66e56 100644 --- a/src/DataFixtures/CoreFixtures.php +++ b/src/DataFixtures/CoreFixtures.php @@ -10,7 +10,6 @@ use App\Entity\GSActor; use App\Entity\LocalGroup; use App\Entity\LocalUser; use App\Entity\Note; -use App\Util\Common; use Doctrine\Bundle\FixturesBundle\Fixture; use Doctrine\Persistence\ObjectManager; use Symfony\Component\HttpFoundation\File\File; @@ -51,7 +50,7 @@ class CoreFixtures extends Fixture $copy_filepath = $filepath . '.copy'; copy($filepath, $copy_filepath); $file = new File($copy_filepath, checkPath: true); - GSFile::sanitizeAndStoreFileAsAttachment($file, dest_dir: Common::config('attachments', 'dir') . 'test/'); + GSFile::sanitizeAndStoreFileAsAttachment($file); $manager->flush(); } }