[ATTACHMENTS] Always store in the same location

This commit is contained in:
Diogo Peralta Cordeiro 2021-08-10 20:24:11 +01:00 committed by Hugo Sales
parent 3f61537140
commit f5175cc59d
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
4 changed files with 6 additions and 15 deletions

View File

@ -106,8 +106,7 @@ class Avatar extends Controller
throw new ClientException('Invalid form'); throw new ClientException('Invalid form');
} }
$attachment = GSFile::sanitizeAndStoreFileAsAttachment( $attachment = GSFile::sanitizeAndStoreFileAsAttachment(
$file, $file
dest_dir: Common::config('attachments', 'dir')
); );
// Delete current avatar if there's one // Delete current avatar if there's one
$avatar = DB::find('avatar', ['gsactor_id' => $gsactor_id]); $avatar = DB::find('avatar', ['gsactor_id' => $gsactor_id]);

View File

@ -122,8 +122,7 @@ END;
$filesize = $f->getSize(); $filesize = $f->getSize();
Event::handle('EnforceQuota', [$actor_id, $filesize]); Event::handle('EnforceQuota', [$actor_id, $filesize]);
$processed_attachments[] = GSFile::sanitizeAndStoreFileAsAttachment( $processed_attachments[] = GSFile::sanitizeAndStoreFileAsAttachment(
$f, $f
dest_dir: Common::config('attachments', 'dir')
); );
} }

View File

@ -29,7 +29,6 @@ use App\Util\Exception\DuplicateFoundException;
use App\Util\Exception\NoSuchFileException; use App\Util\Exception\NoSuchFileException;
use App\Util\Exception\NotFoundException; use App\Util\Exception\NotFoundException;
use App\Util\Exception\ServerException; use App\Util\Exception\ServerException;
use App\Util\Formatting;
use SplFileInfo; use SplFileInfo;
use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\HeaderUtils; use Symfony\Component\HttpFoundation\HeaderUtils;
@ -62,13 +61,8 @@ class GSFile
* *
* @return Attachment * @return Attachment
*/ */
public static function sanitizeAndStoreFileAsAttachment(SplFileInfo $file, public static function sanitizeAndStoreFileAsAttachment(SplFileInfo $file): Attachment
string $dest_dir): 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; $hash = null;
Event::handle('HashFile', [$file->getPathname(), &$hash]); Event::handle('HashFile', [$file->getPathname(), &$hash]);
try { try {
@ -83,12 +77,12 @@ class GSFile
$attachment = Attachment::create([ $attachment = Attachment::create([
'filehash' => $hash, 'filehash' => $hash,
'mimetype' => $mimetype, 'mimetype' => $mimetype,
'filename' => Formatting::removePrefix($dest_dir, Common::config('attachments', 'dir')) . $hash, 'filename' => $hash,
'size' => $file->getSize(), 'size' => $file->getSize(),
'width' => $width, 'width' => $width,
'height' => $height, 'height' => $height,
]); ]);
$file->move($dest_dir, $hash); $file->move(Common::config('attachments', 'dir'), $hash);
DB::persist($attachment); DB::persist($attachment);
Event::handle('AttachmentStoreNew', [&$attachment]); Event::handle('AttachmentStoreNew', [&$attachment]);
} }

View File

@ -10,7 +10,6 @@ use App\Entity\GSActor;
use App\Entity\LocalGroup; use App\Entity\LocalGroup;
use App\Entity\LocalUser; use App\Entity\LocalUser;
use App\Entity\Note; use App\Entity\Note;
use App\Util\Common;
use Doctrine\Bundle\FixturesBundle\Fixture; use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager; use Doctrine\Persistence\ObjectManager;
use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\HttpFoundation\File\File;
@ -51,7 +50,7 @@ class CoreFixtures extends Fixture
$copy_filepath = $filepath . '.copy'; $copy_filepath = $filepath . '.copy';
copy($filepath, $copy_filepath); copy($filepath, $copy_filepath);
$file = new File($copy_filepath, checkPath: true); $file = new File($copy_filepath, checkPath: true);
GSFile::sanitizeAndStoreFileAsAttachment($file, dest_dir: Common::config('attachments', 'dir') . 'test/'); GSFile::sanitizeAndStoreFileAsAttachment($file);
$manager->flush(); $manager->flush();
} }
} }