-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9d38a9b
Showing
12 changed files
with
748 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
root = true | ||
|
||
[*] | ||
end_of_line = lf | ||
insert_final_newline = true | ||
indent_style = space | ||
indent_size = 2 | ||
trim_trailing_whitespace = true | ||
charset = utf-8 | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/.idea | ||
/etc | ||
/studio.json | ||
/tests/coverage | ||
/vendor | ||
.DS_Store | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Release Notes for Chunked File Uploads | ||
|
||
## 1.0.0 - 2019-05-25 | ||
- Initial release. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) Sebastian Lenz | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Chunked File Uploads for Craft CMS | ||
|
||
This plugin enables chunked file uploads in the control panel of | ||
the Craft CMS. It allows users to upload files being larger then the | ||
file upload limit given by your web server. | ||
|
||
|
||
## Requirements | ||
|
||
This plugin requires Craft CMS 3.1 or later. | ||
|
||
|
||
## Installation | ||
|
||
To install the plugin either use the plugin store or follow these | ||
instructions: | ||
|
||
1. Open your terminal and go to your Craft project: | ||
|
||
cd /path/to/project | ||
|
||
2. Then tell Composer to load the plugin: | ||
|
||
composer require sebastianlenz/craft-chunked-uploads | ||
|
||
3. Install the plugin: | ||
|
||
./craft install/plugin chunked-uploads | ||
|
||
|
||
## Settings | ||
|
||
Within your control panel visit the page `Settings` and look | ||
for the plugin in the `Plug-ins` section. The plugin allows | ||
you to both configure the global maximum upload size as well | ||
as upload limits for individual folders. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "sebastianlenz/craft-chunked-uploads", | ||
"description": "Enable large uploads within the Craft CMS control panel", | ||
"type": "craft-plugin", | ||
"license": "MIT", | ||
"require": { | ||
"php": "^7.0", | ||
"craftcms/cms": "^3.1.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"lenz\\craft\\chunkedUploads\\": "src/" | ||
} | ||
}, | ||
"extra": { | ||
"handle": "chunked-uploads", | ||
"name": "Chunked File Uploads", | ||
"developer": "Sebastian Lenz", | ||
"developerUrl": "https://github.com/sebastian-lenz/" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,263 @@ | ||
<?php | ||
|
||
namespace lenz\craft\chunkedUploads; | ||
|
||
use Craft; | ||
use craft\base\Model; | ||
use craft\web\Application; | ||
use craft\web\assets\fileupload\FileUploadAsset; | ||
use craft\web\Request; | ||
use Exception; | ||
use Imagick; | ||
use lenz\craft\chunkedUploads\assets\FileUploadPatch; | ||
use Throwable; | ||
use yii\base\Event; | ||
use yii\base\InvalidConfigException; | ||
use yii\web\HeaderCollection; | ||
use yii\web\View; | ||
|
||
/** | ||
* Class Plugin | ||
* @method Settings getSettings() | ||
*/ | ||
class Plugin extends \craft\base\Plugin | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public $hasCpSettings = true; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
public static $ALLOWED_IMAGE_FORMATS = ['GIF', 'PNG', 'JPEG']; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public static $DEFAULT_FORMAT = 'JPEG'; | ||
|
||
/** | ||
* The name of the uploaded file we are watching for. | ||
*/ | ||
const FILE_NAME = 'assets-upload'; | ||
|
||
|
||
/** | ||
* Plugin constructor. | ||
* | ||
* @param $id | ||
* @param null $parent | ||
* @param array $config | ||
*/ | ||
public function __construct($id, $parent = null, array $config = []) { | ||
parent::__construct($id, $parent, $config); | ||
|
||
if (Craft::$app->request->isCpRequest) { | ||
Event::on(Application::class, Application::EVENT_BEFORE_ACTION, [$this, 'onBeforeAction']); | ||
Event::on(View::class, View::EVENT_END_BODY, [$this, 'onViewEndBody']); | ||
} | ||
} | ||
|
||
/** | ||
* @param Event $event | ||
* @throws Exception | ||
*/ | ||
public function onBeforeAction(Event $event) { | ||
$request = Craft::$app->request; | ||
|
||
if ( | ||
$request->getIsPost() && | ||
$request->getHeaders()->has('content-disposition') && | ||
$request->getHeaders()->has('content-range') && | ||
is_array($_FILES) && | ||
isset($_FILES[self::FILE_NAME]) | ||
) { | ||
if (!$this->processUpload($request)) { | ||
die(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param Event $event | ||
* @throws InvalidConfigException | ||
*/ | ||
public function onViewEndBody(Event $event) { | ||
/** @var View $view */ | ||
$view = $event->sender; | ||
if (array_key_exists(FileUploadAsset::class, $view->assetBundles)) { | ||
$view->registerAssetBundle(FileUploadPatch::class); | ||
} | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
* @throws Exception | ||
*/ | ||
protected function settingsHtml() { | ||
return Craft::$app->view->renderTemplate( | ||
'chunked-uploads/_settings.twig', | ||
[ | ||
'settings' => $this->getSettings(), | ||
] | ||
); | ||
} | ||
|
||
|
||
// Protected methods | ||
// ----------------- | ||
|
||
/** | ||
* @return Model|null | ||
*/ | ||
protected function createSettingsModel() { | ||
return new Settings(); | ||
} | ||
|
||
/** | ||
* @param HeaderCollection $headers | ||
* @return string|null | ||
*/ | ||
private function getContentDisposition(HeaderCollection $headers) { | ||
$contentDisposition = $headers->get('content-disposition'); | ||
return $contentDisposition ? | ||
rawurldecode(preg_replace( | ||
'/(^[^"]+")|("$)/', | ||
'', | ||
$contentDisposition | ||
)) : null; | ||
} | ||
|
||
/** | ||
* @param HeaderCollection $headers | ||
* @return array[] | ||
*/ | ||
private function getContentRange(HeaderCollection $headers) { | ||
$contentRange = $headers->get('content-range'); | ||
$parts = $contentRange | ||
? preg_split('/[^0-9]+/', $contentRange) | ||
: null; | ||
|
||
$offset = is_array($parts) && isset($parts[1]) ? intval($parts[1]) : null; | ||
$size = is_array($parts) && isset($parts[3]) ? intval($parts[3]) : null; | ||
|
||
return [$offset, $size]; | ||
} | ||
|
||
/** | ||
* @param string $uploadedFile | ||
*/ | ||
private function processImage(Request $request, $uploadedFile) { | ||
if (!extension_loaded('imagick')) { | ||
return; | ||
} | ||
|
||
list($maxWidth, $maxHeight) = $this | ||
->getSettings() | ||
->getMaxImageDimension($request->getParam('folderId')); | ||
|
||
if (is_null($maxWidth) && is_null($maxHeight)) { | ||
return; | ||
} | ||
|
||
try { | ||
$hasChanged = false; | ||
$image = new Imagick($uploadedFile); | ||
$format = $image->getImageFormat(); | ||
$geometry = $image->getImageGeometry(); | ||
$nativeWidth = $geometry['width']; | ||
$nativeHeight = $geometry['height']; | ||
$scale = 1; | ||
|
||
if ( | ||
is_array(self::$ALLOWED_IMAGE_FORMATS) && | ||
!in_array($format, self::$ALLOWED_IMAGE_FORMATS) | ||
) { | ||
$hasChanged = true; | ||
$image->setFormat(self::$DEFAULT_FORMAT); | ||
} | ||
|
||
if (!is_null($maxWidth) && $nativeWidth > $maxWidth) { | ||
$scale = $maxWidth / $nativeWidth; | ||
} | ||
|
||
if (!is_null($maxHeight) && $nativeHeight > $maxHeight) { | ||
$scale = min($scale, $maxHeight / $nativeHeight); | ||
} | ||
|
||
if ($scale < 1) { | ||
$hasChanged = true; | ||
$image->resizeImage( | ||
round($nativeWidth * $scale), | ||
round($nativeHeight * $scale), | ||
Imagick::FILTER_LANCZOS, | ||
1 | ||
); | ||
} | ||
|
||
if ($hasChanged) { | ||
$image->setCompressionQuality(100); | ||
file_put_contents($uploadedFile, $image->getImageBlob()); | ||
} | ||
} catch (Throwable $error) { | ||
Craft::error($error->getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* @param Request $request | ||
* @return bool | ||
* @throws Exception | ||
*/ | ||
private function processUpload(Request $request) { | ||
$headers = $request->getHeaders(); | ||
$upload = $_FILES[self::FILE_NAME]; | ||
$uploadedFile = $upload['tmp_name']; | ||
$originalFileName = $this->getContentDisposition($headers); | ||
|
||
list($chunkOffset, $totalSize) = $this->getContentRange($headers); | ||
|
||
if (is_array($uploadedFile)) { | ||
throw new Exception('Multiple files are not supported.'); | ||
} | ||
|
||
if (!is_uploaded_file($uploadedFile)) { | ||
throw new Exception('Invalid upload.'); | ||
} | ||
|
||
if (is_null($originalFileName) || is_null($chunkOffset) || is_null($totalSize)) { | ||
throw new Exception('Missing upload header data.'); | ||
} | ||
|
||
// Recompose chunks | ||
|
||
$tempFile = sys_get_temp_dir() . '/craft_upload_chunks_' . md5($originalFileName); | ||
if ($chunkOffset > 0) { | ||
$uploadedSize = filesize($tempFile); | ||
if ($uploadedSize != $chunkOffset) { | ||
throw new Exception('Invalid chunk offset.'); | ||
} | ||
|
||
file_put_contents($tempFile, fopen($uploadedFile, 'r'), FILE_APPEND); | ||
} else { | ||
if (file_exists($tempFile)) { | ||
unlink($tempFile); | ||
} | ||
|
||
move_uploaded_file($uploadedFile, $tempFile); | ||
} | ||
|
||
// Check for upload completion | ||
|
||
clearstatcache(); | ||
$uploadedSize = filesize($tempFile); | ||
$isFinished = $uploadedSize == $totalSize; | ||
if ($isFinished) { | ||
rename($tempFile, $uploadedFile); | ||
$this->processImage($request, $uploadedFile); | ||
} | ||
|
||
return $isFinished; | ||
} | ||
} |
Oops, something went wrong.