-
Notifications
You must be signed in to change notification settings - Fork 1
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 97fe563
Showing
8 changed files
with
279 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,32 @@ | ||
# CRAFT ENVIRONMENT | ||
.env.php | ||
.env.sh | ||
.env | ||
|
||
# COMPOSER | ||
/vendor | ||
|
||
# BUILD FILES | ||
/bower_components/* | ||
/node_modules/* | ||
/build/* | ||
/yarn-error.log | ||
|
||
# MISC FILES | ||
.cache | ||
.DS_Store | ||
.idea | ||
.project | ||
.settings | ||
*.esproj | ||
*.sublime-workspace | ||
*.sublime-project | ||
*.tmproj | ||
*.tmproject | ||
.vscode/* | ||
!.vscode/settings.json | ||
!.vscode/tasks.json | ||
!.vscode/launch.json | ||
!.vscode/extensions.json | ||
config.codekit3 | ||
prepros-6.config |
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,8 @@ | ||
# ForceDownload Changelog | ||
|
||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). | ||
|
||
## 1.0.0 - 2017-08-02 | ||
- 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,9 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2017 MDXDave | ||
|
||
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,35 @@ | ||
# ForceDownload plugin for Craft CMS 3.x | ||
|
||
Simple Downloader for Craft CMS 3.0. | ||
|
||
## Requirements | ||
|
||
This plugin requires Craft CMS 3.0.0-beta.23 or later. | ||
|
||
## Installation | ||
|
||
To install the plugin, 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 mdxdave/force-download | ||
|
||
3. In the Control Panel, go to Settings → Plugins and click the “Install” button for ForceDownload. | ||
|
||
## Using ForceDownload | ||
|
||
Just use the following code in your Twig template file (whereas __filename__ is an asset field): | ||
|
||
{{ entry.filename[0]|forceDownload }} | ||
|
||
To use a rudimental download counter, use the fieldname as 2nd param (the __downloadCounter__ field will be inceremented by 1): | ||
|
||
{{ entry.filename[0]|forceDownload("downloadCounter") }} | ||
|
||
|
||
|
||
Brought to you by [MDXDave](https://mdxdave.de) |
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,50 @@ | ||
{ | ||
"name": "mdxdave/force-download", | ||
"description": "Force download of assets", | ||
"type": "craft-plugin", | ||
"version": "1.0.0", | ||
"keywords": [ | ||
"craft", | ||
"cms", | ||
"craftcms", | ||
"craft-plugin", | ||
"timeago" | ||
], | ||
"support": { | ||
"docs": "https://github.com/mdxdave/force-download/blob/master/README.md", | ||
"issues": "https://github.com/mdxdave/force-download/issues" | ||
}, | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "MDXDave", | ||
"homepage": "https://mdxdave.de" | ||
} | ||
], | ||
"require": { | ||
"craftcms/cms": "~3.0.0-beta.23" | ||
}, | ||
"config": { | ||
"optimize-autoloader": true | ||
}, | ||
"repositories": [ | ||
{ | ||
"type": "composer", | ||
"url": "https://asset-packagist.org" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"mdxdave\\forcedownload\\": "src/" | ||
} | ||
}, | ||
"extra": { | ||
"name": "ForceDownload", | ||
"handle": "force-download", | ||
"schemaVersion": "1.0.0", | ||
"hasCpSettings": false, | ||
"hasCpSection": false, | ||
"changelogUrl": "https://raw.githubusercontent.com/mdxdave/force-download/master/CHANGELOG.md", | ||
"class": "mdxdave\\forcedownload\\ForceDownload" | ||
} | ||
} |
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,41 @@ | ||
<?php | ||
/** | ||
* ForceDownload Plugin for Craft CMS 3.x | ||
* | ||
* @link https://mdxdave.de | ||
* @copyright Copyright (c) 2017 MDXDave | ||
*/ | ||
|
||
namespace mdxdave\forcedownload; | ||
|
||
use Craft; | ||
use craft\base\Plugin; | ||
use craft\services\Plugins; | ||
use craft\events\PluginEvent; | ||
use craft\web\UrlManager; | ||
use craft\events\RegisterUrlRulesEvent; | ||
|
||
use yii\base\Event; | ||
|
||
class ForceDownload extends Plugin | ||
{ | ||
|
||
public static $plugin; | ||
|
||
public function init() | ||
{ | ||
parent::init(); | ||
self::$plugin = $this; | ||
|
||
Craft::$app->view->twig->addExtension(new twigextensions\ForceDownloadTwigExtension()); | ||
|
||
Craft::info( | ||
Craft::t( | ||
'force-download', | ||
'{name} plugin loaded', | ||
['name' => $this->name] | ||
), | ||
__METHOD__ | ||
); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,52 @@ | ||
<?php | ||
/** | ||
* ForceDownload Plugin for Craft CMS 3.x | ||
* | ||
* @link https://mdxdave.de | ||
* @copyright Copyright (c) 2017 MDXDave | ||
*/ | ||
|
||
namespace mdxdave\forcedownload\twigextensions; | ||
|
||
use mdxdave\forcedownload\ForceDownload; | ||
use Craft; | ||
|
||
class ForceDownloadTwigExtension extends \Twig_Extension | ||
{ | ||
public function getName() | ||
{ | ||
return 'ForceDownload'; | ||
} | ||
|
||
public function getFilters() | ||
{ | ||
return [ | ||
new \Twig_SimpleFilter('forceDownload', [$this, 'forceDownload']), | ||
]; | ||
} | ||
|
||
function forceDownload($asset, $downloadCounter=null) { | ||
if($downloadCounter){ | ||
$download = Craft::$app->urlManager->getMatchedElement(); | ||
$download->setFieldValue($downloadCounter, $download->getFieldValue($downloadCounter)+1); | ||
Craft::$app->elements->saveElement($download); | ||
} | ||
$path = $this->_getFullDirectoryPath($asset); | ||
$filename = $asset->filename; | ||
$filepath = $path . '/' . $filename; | ||
header('Content-type: '.$asset->getMimeType()); | ||
header('Content-Disposition: attachment; filename="' . $filename . '"'); | ||
header('Content-Transfer-Encoding: binary'); | ||
header('Content-Length: ' . $asset->size); | ||
header('Accept-Ranges: bytes'); | ||
readfile($filepath); | ||
exit(); | ||
} | ||
|
||
private function _getFullDirectoryPath($file) | ||
{ | ||
$volumeId = $file->volumeId; | ||
$volume = Craft::$app->getVolumes()->getVolumeById($volumeId); | ||
return $volume->path; | ||
} | ||
} |