summaryrefslogtreecommitdiff
path: root/static/classes
diff options
context:
space:
mode:
authorEric Johansson (neku) <neku@pomf.se>2015-02-05 20:06:08 +0100
committerEric Johansson (neku) <neku@pomf.se>2015-02-05 20:06:08 +0100
commitb816236168e74309be4a298ff0a4644a732dc9f3 (patch)
tree1497dced7d755362dcdfd73b7e11d78b04597f2d /static/classes
parentc359aa20cd0876e1e497aca1ff19e4c54287b6c2 (diff)
parentfd98101d073805fc42474a00bca640a618f99de6 (diff)
Merge pull request #27 from cenci0/master
Minor code refactoring and improvements
Diffstat (limited to 'static/classes')
-rwxr-xr-xstatic/classes/Response.class.php98
-rwxr-xr-xstatic/classes/UploadException.class.php45
-rw-r--r--static/classes/UploadedFile.class.php13
3 files changed, 153 insertions, 3 deletions
diff --git a/static/classes/Response.class.php b/static/classes/Response.class.php
new file mode 100755
index 0000000..41f946c
--- /dev/null
+++ b/static/classes/Response.class.php
@@ -0,0 +1,98 @@
+<?php
+
+class Response {
+ private $type;
+
+ public function __construct ($response_type = null) {
+ switch ($response_type) {
+ case 'csv':
+ case 'gyazo':
+ header('Content-Type: text/plain; charset=UTF-8');
+ $this->type = $response_type;
+ break;
+ default:
+ header('Content-Type: application/json; charset=UTF-8');
+ $this->type = 'json';
+ break;
+ }
+ }
+
+ public function error ($code, $desc) {
+ $response = null;
+
+ switch ($this->type) {
+ case 'csv':
+ $response = $this->csv_error($desc);
+ break;
+ case 'gyazo':
+ $response = $this->gyazo_error($code, $desc);
+ break;
+ default:
+ $response = $this->json_error($code, $desc);
+ break;
+ }
+
+ http_response_code($code);
+ echo $response;
+ }
+
+ public function send ($files) {
+ $response = null;
+
+ switch ($this->type) {
+ case 'csv':
+ $response = $this->csv_success($files);
+ break;
+ case 'gyazo':
+ $response = $this->gyazo_success($files);
+ break;
+ default:
+ $response = $this->json_success($files);
+ break;
+ }
+
+ http_response_code(200);
+ echo $response;
+ }
+
+ private static function csv_error ($description) {
+ return "error\n" . $description . "\n";
+ }
+
+ private static function csv_success ($files) {
+ $result = "name,url,hash,size\n";
+ foreach ($files as $file) {
+ $result .= $file['name'] . "," .
+ $file['url'] . "," .
+ $file['hash'] . "," .
+ $file['size'] . "\n";
+ }
+
+ return $result;
+ }
+
+ private static function gyazo_error ($code, $description) {
+ return "ERROR: (" . $code . ") " . $description;
+ }
+
+ private static function gyazo_success ($files) {
+ return POMF_URL . $files[0]['url'];
+ }
+
+ private static function json_error ($code, $description) {
+ return json_encode(array(
+ 'success' => false,
+ 'errorcode' => $code,
+ 'description' => $description
+ ));
+ }
+
+ private static function json_success ($files) {
+ return json_encode(array(
+ 'success' => true,
+ 'files' => $files
+ ));
+ }
+}
+
+?> \ No newline at end of file
diff --git a/static/classes/UploadException.class.php b/static/classes/UploadException.class.php
new file mode 100755
index 0000000..b1b4839
--- /dev/null
+++ b/static/classes/UploadException.class.php
@@ -0,0 +1,45 @@
+<?php
+
+// Exception class extension from: http://php.net/manual/en/features.file-upload.errors.php#89374
+// Handles files error returning a proper error description
+
+class UploadException extends Exception {
+ public function __construct($code) {
+ $message = $this->codeToMessage($code);
+ parent::__construct($message, 500);
+ }
+
+ private function codeToMessage($code) {
+ switch ($code) {
+ case UPLOAD_ERR_INI_SIZE:
+ $message = "The uploaded file exceeds the upload_max_filesize directive in php.ini";
+ break;
+ case UPLOAD_ERR_FORM_SIZE:
+ $message = "The uploaded file exceeds the MAX_FILE_SIZE directive that was " .
+ "specified in the HTML form";
+ break;
+ case UPLOAD_ERR_PARTIAL:
+ $message = "The uploaded file was only partially uploaded";
+ break;
+ case UPLOAD_ERR_NO_FILE:
+ $message = "No file was uploaded";
+ break;
+ case UPLOAD_ERR_NO_TMP_DIR:
+ $message = "Missing a temporary folder";
+ break;
+ case UPLOAD_ERR_CANT_WRITE:
+ $message = "Failed to write file to disk";
+ break;
+ case UPLOAD_ERR_EXTENSION:
+ $message = "File upload stopped by extension";
+ break;
+
+ default:
+ $message = "Unknown upload error";
+ break;
+ }
+ return $message;
+ }
+}
+
+?> \ No newline at end of file
diff --git a/static/classes/UploadedFile.class.php b/static/classes/UploadedFile.class.php
index 5b7ba9b..102d6d9 100644
--- a/static/classes/UploadedFile.class.php
+++ b/static/classes/UploadedFile.class.php
@@ -4,9 +4,9 @@ class UploadedFile {
/* Public attributes */
public $name;
public $mime;
+ public $size;
public $tempfile;
public $error;
- public $size;
/* Cached checksums */
private $sha1;
@@ -14,17 +14,24 @@ class UploadedFile {
/**
* Generates the SHA1 or returns the cached SHA1 hash for the file.
+ *
+ * @return string
*/
public function get_sha1 () {
- if (! $this->sha1) $this->sha1 = sha1_file($this->tempfile);
+ if (!$this->sha1)
+ $this->sha1 = sha1_file($this->tempfile);
return $this->sha1;
}
+
/**
* Generates the CRC32 or returns the cached CRC32 hash for the file.
+ *
+ * @return string
*/
public function get_crc32 () {
- if (! $this->crc32) $this->crc32 = hash_file('crc32b', $this->tempfile);
+ if (!$this->crc32)
+ $this->crc32 = hash_file('crc32b', $this->tempfile);
return $this->crc32;
}