Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Commit a9d6a4b

Browse files
committed
DownloadAction created
1 parent 4b168e8 commit a9d6a4b

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

DownloadAction.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
/**
3+
* @link https://github.com/yii2tech
4+
* @copyright Copyright (c) 2015 Yii2tech
5+
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
6+
*/
7+
8+
namespace yii2tech\filestorage;
9+
10+
use Yii;
11+
use yii\base\Action;
12+
use yii\di\Instance;
13+
use yii\helpers\FileHelper;
14+
use yii\web\NotFoundHttpException;
15+
use yii\web\Response;
16+
17+
/**
18+
* DownloadAction
19+
*
20+
* @author Paul Klimov <klimov.paul@gmail.com>
21+
* @since 1.1.0
22+
*/
23+
class DownloadAction extends Action
24+
{
25+
/**
26+
* @var StorageInterface|array|string file storage object or the application component ID of the file storage.
27+
*/
28+
public $fileStorage = 'fileStorage';
29+
/**
30+
* @var array list of bucket names that this action should allow to use. If this property is not set,
31+
* then the action allows to all buckets available in [[fileStorage]], unless they are listed in [[exceptBuckets]].
32+
* If a bucket name appears in both [[onlyBuckets]] and [[exceptBuckets]], this action will NOT allow it to be used.
33+
*/
34+
public $onlyBuckets;
35+
/**
36+
* @var array list of the bucket names, that this action should not allow to use.
37+
* @see onlyBuckets
38+
*/
39+
public $exceptBuckets = [];
40+
41+
42+
/**
43+
* Runs the action.
44+
* @param string $bucket name of the file source bucket
45+
* @param string $filename name of the file to be downloaded.
46+
* @return Response response.
47+
* @throws NotFoundHttpException if bucket or file does not exist.
48+
*/
49+
public function run($bucket, $filename)
50+
{
51+
if (!$this->isBucketAllowed($bucket)) {
52+
throw new NotFoundHttpException("Bucket '{$bucket}' does not exist.");
53+
}
54+
55+
$this->fileStorage = Instance::ensure($this->fileStorage, 'yii2tech\filestorage\StorageInterface');
56+
57+
if (!$this->fileStorage->hasBucket($bucket)) {
58+
throw new NotFoundHttpException("Bucket '{$bucket}' does not exist.");
59+
}
60+
61+
$bucket = $this->fileStorage->getBucket($bucket);
62+
63+
if (!$bucket->fileExists($filename)) {
64+
throw new NotFoundHttpException("File '{$filename}' does not exist at bucket '{$bucket->getName()}' does not exist.");
65+
}
66+
67+
$response = Yii::$app->getResponse();
68+
$response->content = $bucket->getFileContent($filename);
69+
70+
$response->format = Response::FORMAT_RAW;
71+
72+
$mimeType = FileHelper::getMimeTypeByExtension($filename);
73+
if (empty($mimeType)) {
74+
$mimeType = 'application/octet-stream';
75+
}
76+
77+
$response->getHeaders()->add('Content-Type', $mimeType);
78+
79+
return $response;
80+
}
81+
82+
/**
83+
* Returns a value indicating whether the download from the specified bucket is allowed or not.
84+
* @param string $bucketName the name of the bucket.
85+
* @return boolean whether the download from the bucket is allowed or not.
86+
*/
87+
protected function isBucketAllowed($bucketName)
88+
{
89+
return !in_array($bucketName, $this->exceptBuckets, true) && (empty($this->onlyBuckets) || in_array($bucketName, $this->onlyBuckets, true));
90+
}
91+
}

0 commit comments

Comments
 (0)