summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Lejeck <peter.lejeck@gmail.com>2013-12-01 06:13:27 +0100
committerPeter Lejeck <peter.lejeck@gmail.com>2013-12-01 06:13:27 +0100
commit1d0d81c1e89390bd9d31b3a965a75803041b41cb (patch)
treeaf6db4be704e6d47f6b520cf8f92e9efc3eaf517
parent74fb6b2508f80acb820ab667c463e17468b71251 (diff)
Add response types via ?out=type
Types are "gyazo", "csv", and "json" (default)
-rw-r--r--static/upload.php75
1 files changed, 57 insertions, 18 deletions
diff --git a/static/upload.php b/static/upload.php
index 11ed6b9..a3274ff 100644
--- a/static/upload.php
+++ b/static/upload.php
@@ -116,32 +116,73 @@ function refiles ($files) {
return $out;
}
+/**
+ * Give a response that gyazo understands
+ */
+function respond_gyazo ($code, $files) {
+ if ($files instanceof Exception) {
+ echo "ERROR: " . $files->getMessage();
+ } else {
+ echo 'http://a.pomf.se/' . $files[0]['url'];
+ }
+}
/**
- * Responds to a request in JSON form.
+ * Responds to a request in CSV form.
*/
-function respond ($code, $res = null) {
- if (is_int($code)) { // If the code is an integer, we assume it's a response code
- http_response_code($code);
- } else { // Otherwise we just assume you left off the code and use the default and shift
- http_response_code(200);
- $res = $code;
+function respond_csv ($code, $files) {
+ if ($files instanceof Exception) {
+ echo "error\n";
+ echo $files->getMessage() . "\n";
+ } else {
+ echo "name,url,hash,size\n";
+ foreach ($files as $file) {
+ echo "${file['name']},${file['url']},${file['hash']},${file['size']}\n";
+ }
}
+}
+/**
+ * Responds to a request in JSON form.
+ */
+function respond_json ($code, $files) {
// Now we send the response based on the type
- if ($res instanceof Exception) {
+ if ($files instanceof Exception) {
// If it's an Exception, we put the message in the error field
echo json_encode(array(
'success' => false,
- 'error' => $res->getMessage()
+ 'error' => $files->getMessage()
));
- } elseif (is_array($res)) {
- // If it's an Array, we merge it with 'success' => true.
- // Note that $res is later in the list, so you can override that.
- echo json_encode(array_merge(array(
+ } elseif (is_array($files)) {
+ echo json_encode(array(
'success' => true,
- 'error' => null
- ), $res));
+ 'error' => null,
+ 'files' => $files
+ ));
+ }
+}
+
+/**
+ * Determines the proper response function based on $_GET['output]
+ */
+function respond ($code, $files = null) {
+ if (is_int($code)) { // If the code is an integer, we assume it's a response code
+ http_response_code($code);
+ } else { // Otherwise we just use the default and shift
+ http_response_code(200);
+ $files = $code;
+ }
+
+ switch ($_GET['output']) {
+ case 'gyazo':
+ respond_gyazo($code, $files);
+ break;
+ case 'csv':
+ respond_csv($code, $files);
+ break;
+ case 'json':
+ default:
+ respond_json($code, $files);
}
}
@@ -151,9 +192,7 @@ if (isset($_FILES['files'])) {
foreach ($uploads as $upload) {
$out[] = upload_file($upload);
}
- respond(array(
- 'files' => $out
- ));
+ respond($out);
} catch (Exception $e) {
respond(500, $e);
}