Skip to content

Commit

Permalink
Add support for uploading SVGs
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeTowers committed Jul 7, 2023
1 parent edf2087 commit 186d85d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
22 changes: 16 additions & 6 deletions src/Database/Attach/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Winter\Storm\Exception\ApplicationException;
use Winter\Storm\Network\Http;
use Winter\Storm\Support\Facades\File as FileHelper;
use Winter\Storm\Support\Svg;

/**
* File attachment model
Expand Down Expand Up @@ -834,31 +835,40 @@ protected function putFile($sourcePath, $destinationFileName = null)
$destinationFileName = $this->disk_name;
}

$destinationPath = $this->getStorageDirectory() . $this->getPartitionDirectory();
$destinationFolder = $this->getStorageDirectory() . $this->getPartitionDirectory();
$destinationPath = $destinationFolder . $destinationFileName;

// Filter SVG files
if (pathinfo($destinationPath, PATHINFO_EXTENSION) === 'svg') {
file_put_contents($sourcePath, Svg::extract($sourcePath));
}

pathinfo($destinationPath, PATHINFO_EXTENSION);

if (!$this->isLocalStorage()) {
return $this->copyLocalToStorage($sourcePath, $destinationPath . $destinationFileName);
return $this->copyLocalToStorage($sourcePath, $destinationPath);
}

/*
* Using local storage, tack on the root path and work locally
* this will ensure the correct permissions are used.
*/
$destinationPath = $this->getLocalRootPath() . '/' . $destinationPath;
$destinationFolder = $this->getLocalRootPath() . '/' . $destinationFolder;
$destinationPath = $destinationFolder . $destinationFileName;

/*
* Verify the directory exists, if not try to create it. If creation fails
* because the directory was created by a concurrent process then proceed,
* otherwise trigger the error.
*/
if (
!FileHelper::isDirectory($destinationPath) &&
!FileHelper::makeDirectory($destinationPath, 0777, true, true)
!FileHelper::isDirectory($destinationFolder) &&
!FileHelper::makeDirectory($destinationFolder, 0777, true, true)
) {
trigger_error(error_get_last()['message'], E_USER_WARNING);
}

return FileHelper::copy($sourcePath, $destinationPath . $destinationFileName);
return FileHelper::copy($sourcePath, $destinationPath);
}

/**
Expand Down
13 changes: 8 additions & 5 deletions src/Filesystem/Definitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ protected function defaultExtensions(): array
'png',
'webp',
'gif',
'svg',
'js',
'map',
'ico',
Expand Down Expand Up @@ -147,7 +148,7 @@ protected function defaultExtensions(): array
'webm',
'mkv',
'rar',
'zip'
'zip',
];
}

Expand All @@ -167,6 +168,7 @@ protected function assetExtensions(): array
'png',
'webp',
'gif',
'svg',
'ico',
'css',
'js',
Expand All @@ -178,7 +180,7 @@ protected function assetExtensions(): array
'md',
'less',
'sass',
'scss'
'scss',
];
}

Expand All @@ -197,7 +199,8 @@ protected function imageExtensions(): array
'bmp',
'png',
'webp',
'gif'
'gif',
'svg',
];
}

Expand All @@ -217,7 +220,7 @@ protected function videoExtensions(): array
'mpg',
'mpeg',
'mkv',
'webm'
'webm',
];
}

Expand All @@ -235,7 +238,7 @@ protected function audioExtensions(): array
'wav',
'wma',
'm4a',
'ogg'
'ogg',
];
}
}

0 comments on commit 186d85d

Please sign in to comment.