Skip to content

Commit

Permalink
Merge pull request #54 from craftcms/bugfix/special-chars-in-path
Browse files Browse the repository at this point in the history
Handle special characters that have been encoded from the presigned URL
  • Loading branch information
timkelty authored Oct 3, 2024
2 parents 7d70774 + 3c03912 commit 75bf08f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/controllers/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ public function actionCreateAsset(): Response

// Setting these so that Asset::_relocateFile doesn't try to download
$asset->folderId = $folder->id;
$asset->folderPath = $folder->path;

// Handle special characters that have been encoded from the presigned URL
$asset->folderPath = Fs::urlEncodePathSegments($folder->path);

if (!$selectionCondition) {
$asset->newFilename = $targetFilename;
Expand Down
11 changes: 11 additions & 0 deletions src/fs/Fs.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use DateTime;
use DateTimeInterface;
use Generator;
use Illuminate\Support\Collection;
use League\Flysystem\AwsS3V3\AwsS3V3Adapter;
use League\Flysystem\FilesystemException;
use League\Flysystem\UnableToCopyFile;
Expand Down Expand Up @@ -542,4 +543,14 @@ public function replaceMetadata(string $path, array $config = []): void
throw new FsException($exception->getMessage(), 0, $exception);
}
}

/**
* S3 encodes path segments when generating presigned PUT urls, so we need to do the same.
*/
public static function urlEncodePathSegments(string $path): string
{
return Collection::make(explode('/', $path))
->map(fn($segment) => rawurlencode($segment))
->implode('/');
}
}

0 comments on commit 75bf08f

Please sign in to comment.