forked from 2amigos/yii2-file-upload-widget
-
Notifications
You must be signed in to change notification settings - Fork 1
/
File.php
102 lines (85 loc) · 3.06 KB
/
File.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/**
* @copyright Copyright (c) 2013 2amigOS! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace dosamigos\fileupload;
use Aws\S3\Enum\CannedAcl;
use yii\web\UploadedFile;
use dosamigos\fileupload\ImageHelper;
use Yii;
/**
* File is the model for managing an UploadedFile
*
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @author Andrew Blake <admin@newzealandfishing.com>
* @package dosamigos\fileupload
*/
class File extends \yii\base\Model
{
const LARGE_IMAGE = '400x400';
const SMALL_IMAGE = '80x80';
const ISPRIVATE = '+10 minutes';
const ISPUBLIC = null;
/**
* @var UploadedFile|Null file attribute
*/
public $file;
public $name;
public $basePath;
public $privacy = self::ISPRIVATE;
public function save()
{
if ($this->file && $this->file instanceof UploadedFile && !empty($this->file->tempName)) {
$large = ImageHelper::saveTemporaryImage($this->file->tempName, $this->file->name, self::LARGE_IMAGE, $this->basePath);
$small = ImageHelper::saveTemporaryImage($this->file->tempName, $this->file->name, self::SMALL_IMAGE, $this->basePath);
$manager = Yii::$app->resourceManager;
$options = [
'Bucket' => $manager->bucket,
'Key' => $this->basePath . '/' . self::LARGE_IMAGE . '/' . $this->file->name,
'SourceFile' => $large,
'ACL' => CannedAcl::PUBLIC_READ
];
$manager->getClient()->putObject($options);
$options['Key'] = $this->basePath . '/' . self::SMALL_IMAGE . '/' . $this->file->name;
$options['SourceFile'] = $small;
$manager->getClient()->putObject($options);
@unlink($large);
@unlink($small);
$this->name = $this->file->name;
}
}
public function getImageUrl($type = self::LARGE_IMAGE)
{
return Yii::$app->resourceManager->getUrl(
$this->basePath . '/' . $type . '/' . $this->name, $this->privacy
);
}
public function delete()
{
$manager = Yii::$app->resourceManager;
$manager->delete($this->basePath . '/' . self::SMALL_IMAGE . '/' . $this->name);
$manager->delete($this->basePath . '/' . self::LARGE_IMAGE . '/' . $this->name);
}
public function jqueryFileUploadResponse()
{
if (isset($file->hasErrors)) {
// just the first error for now for simplicity
return [
'name' => $file->file->name,
'size' => $file->file->size,
"error" => $file->firstErrors['file']
];
}
return [
'name' => $this->name = $this->file->name,
'type' => $this->file->type,
'size' => $this->file->size,
'url' => $this->getImageUrl(),
'thumbnailUrl' => $this->getImageUrl(File::SMALL_IMAGE),
'deleteUrl' => $this->file->name,
'deleteType' => 'POST'
];
}
}