Skip to content

Commit

Permalink
Merge pull request #17 from outl1ne/feature/2
Browse files Browse the repository at this point in the history
Encode file names for storage
  • Loading branch information
KasparRosin authored Sep 21, 2022
2 parents 156b649 + e69deb1 commit ea8f415
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/MediaHandler/Support/FileHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ public static function getFileHash(string $path, string $disk = null): string

public static function sanitizeFileName($fileName)
{
return str_replace(['#', '/', '\\', ' ', '?', '='], '-', $fileName);
[$extension, $name] = explode('.', strrev($fileName), 2);
$sanitizedName = urlencode(str_replace(['#', '/', '\\', ' ', '?', '=', '.'], '-', $name));

return strrev("{$extension}.{$sanitizedName}");
}

// Returns [$fileName, $extension]
Expand Down
7 changes: 7 additions & 0 deletions src/MediaHandler/Support/FileNamer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@ public function formatConversionFileName(string $fileName, string $extension, $c
{
return "{$fileName}.{$conversion}.{$extension}";
}

public static function encode(string $fileName): string
{
// For backwards compatibility.
// Encodes fileName while avoiding double encoding.
return urlencode(urldecode($fileName));
}
}
5 changes: 3 additions & 2 deletions src/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Outl1ne\NovaMediaHub\MediaHub;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use Outl1ne\NovaMediaHub\MediaHandler\Support\FileNamer;

class Media extends Model
{
Expand Down Expand Up @@ -38,7 +39,7 @@ public function getConversionsPathAttribute()

public function getUrlAttribute()
{
return Storage::disk($this->disk)->url("{$this->path}{$this->file_name}");
return Storage::disk($this->disk)->url(FileNamer::encode("{$this->path}{$this->file_name}"));
}

public function getThumbnailUrlAttribute()
Expand All @@ -49,7 +50,7 @@ public function getThumbnailUrlAttribute()
$thumbnailName = $this->conversions[$thumbnailConversionName] ?? null;
if (!$thumbnailName) return null;

return Storage::disk($this->conversions_disk)->url($this->conversionsPath . $thumbnailName);
return Storage::disk($this->conversions_disk)->url(FileNamer::encode($this->conversionsPath . $thumbnailName));
}

public function formatForNova()
Expand Down

0 comments on commit ea8f415

Please sign in to comment.