-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuploadschedule.php
More file actions
73 lines (55 loc) · 1.79 KB
/
uploadschedule.php
File metadata and controls
73 lines (55 loc) · 1.79 KB
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
<?php
/**
* Created by PhpStorm.
* User: Wei Hong
* Date: 15/3/2018
* Time: 11:42 PM
*/
require_once $_SERVER['DOCUMENT_ROOT'] . "/vendor/autoload.php";
use MicrosoftAzure\Storage\Blob\BlobRestProxy;
#JSON Response
$jsonResponseArr = array();
#Restricts to POST
if ($_SERVER['REQUEST_METHOD'] != "POST") {
onError('API only accepts POST request');
}
#Reads Config file
$config = parse_ini_file("blobstorageconfig.ini", true);
if ($config === false) {
onError('Error reading Blob Storage config file');
}
$accountName = $config['account']['name'];
$accountKey = $config['account']['key'];
#Attemps to retrieve file uploaded
if ($_FILES['schedule']['error'] != UPLOAD_ERR_OK) {
onError($_FILES['schedule']['error']);
}
$fileName = $_FILES['schedule']['name'];
$file = $_FILES['schedule']['tmp_name'];
#Blob Storage Related
$connectionStr = "DefaultEndpointsProtocol=http;AccountName={$accountName};AccountKey={$accountKey}";
$blobClient = BlobRestProxy::createBlobService($connectionStr);
$containerName = $config['container']['name'];
#Uploading to blob storage
$content = fopen($file, "r");
if (!$content) {
onError('Error opening file to convert to blob for uploading');
}
$blobClient->createBlockBlob($containerName, $fileName, $content);
$jsonResponseArr['status'] = 'ok';
$jsonResponseArr['statusCode'] = '200';
$jsonResponseArr['link'] = "https://eieio.blob.core.windows.net/{$containerName}/{$fileName}";
printJSON();
function onError($reason) {
global $jsonResponseArr;
$jsonResponseArr['status'] = 'error';
$jsonResponseArr['statusCode'] = '503';
$jsonResponseArr['reason'] = $reason;
printJSON();
}
function printJSON() {
header('Content-type: text/json');
global $jsonResponseArr;
print json_encode($jsonResponseArr, JSON_PRETTY_PRINT);
die();
}