-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions-aws.php
74 lines (55 loc) · 1.7 KB
/
functions-aws.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
<?
require_once(APP_PATH . '/lib/aws/aws-autoloader.php');
$key = getenv('AWSAccessKeyId');
$secret = getenv('AWSSecretKey');
$region = getenv('AWS_REGION');
$credentials = new Aws\Credentials\Credentials($key, $secret);
$options = [
'version' => 'latest',
'region' => $region,
'credentials' => $credentials
];
$client = new Aws\S3\S3Client($options);
// Register the stream wrapper from an S3Client object
$client->registerStreamWrapper();
$x = 123;
function upload_to_s3($from_local_file, $to_s3_bucket_key_url) {
if (substr($to_s3_bucket_key_url, 0, 5) !== 's3://') {
return "bad dest";
}
$stream = fopen($to_s3_bucket_key_url, 'w');
$data = file_get_contents($from_local_file);
$result = fwrite($stream, $data);
fclose($stream);
return $result;
}
function s3_path_of_hash($hash) {
$bucket = AWS_BUCKET;
return "s3://${bucket}/sha256/${hash}";
}
//should this be in functions.php instead ?
function serve_asset($sha256, $opts = ['attachment' => false]) {
//get the actual document
$path = s3_path_of_hash($sha256);
//pp($path,'path');
//exit;
if (empty($path) || !file_exists($path)) {
error_404();
}
// get the file's mime type to send the correct content type header
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime_type = finfo_file($finfo, $path);
$public_name = 'Untitled';
if (isset($opts['title'])) {
$public_name = $opts['title'];
}
// send the headers
$dispo = $opts['attachment'] ? 'attachment' : 'inline';
header("Content-Disposition: $dispo; filename=\"$public_name\";");
header("Content-Type: $mime_type");
header('Content-Length: ' . filesize($path));
// stream the file
$fp = fopen($path, 'rb');
fpassthru($fp);
exit;
}