Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MDXDave committed Aug 1, 2017
0 parents commit 97fe563
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .gitignore
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
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
9 changes: 9 additions & 0 deletions LICENSE.md
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.
35 changes: 35 additions & 0 deletions README.md
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)
50 changes: 50 additions & 0 deletions composer.json
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"
}
}
41 changes: 41 additions & 0 deletions src/ForceDownload.php
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__
);
}
}
52 changes: 52 additions & 0 deletions src/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions src/twigextensions/ForceDownloadTwigExtension.php
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;
}
}

0 comments on commit 97fe563

Please sign in to comment.