This repository has been archived by the owner on Sep 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CiiAWSUploader.php
68 lines (58 loc) · 1.84 KB
/
CiiAWSUploader.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
<?php
/**
* Allows CiiMS to upload files to AWS S3 CDN
*/
Yii::import('cii.utilities.CiiUploader');
class CiiAWSUploader extends CiiUploader
{
public function upload()
{
$key = defined('CII_CONFIG') ? CII_CONFIG : $_SERVER['HTTP_HOST'];
$check = $this->verifyFile();
if (isset($check['error']))
return $check;
$filename = $check['success'];
$fullFileName = $filename.'.'.$this->file->getExtension();
// Instantiate an S3 client
$client = Aws\S3\S3Client::factory(array(
'key' => $this->AWS_ACCESS_KEY,
'secret' => $this->AWS_SECRET_ACCESS_KEY,
'region' => $this->region
));
$factory = new CryptLib\Random\Factory;
$file = preg_replace('/[^\da-z]/i', '', $factory->getLowStrengthGenerator()->generateString(32));
$cdnFilename = $file.'.'.$this->file->getExtension();
try {
$result = $client->putObject(array(
'Bucket' => $this->bucket,
'Key' => $key.'/'.$cdnFilename,
'SourceFile' => $this->file->tmp_name,
'ACL' => 'public-read',
'ContentType' => 'image/'.$this->file->getExtension()
));
} catch (Exception $e) {
return array(
'error' => $e->getMessage()
);
}
if ($this->cdn_domain != NULL)
{
$url = parse_url($result['ObjectURL']);
return array('success' => true, 'url' => $this->cdn_domain . $this->getCdnURI($url['path']), 'filename' => $file);
}
return array('success' => true, 'url' => $result['ObjectURL'], 'filename' => $file);
}
/**
* Retrieves a clean version of the CDN URI
* @param string $path
* @return string
*/
private function getCdnURI($path)
{
$cleancdn = explode('/', $path);
unset($cleancdn[0]);
unset($cleancdn[1]);
reset($cleancdn);
return implode($cleancdn, '/');
}
}