[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');
}
$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]);

View File

@ -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
);
}

View File

@ -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]);
}

View File

@ -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();
}
}