diff options
| author | cenci0 <alchimist94@gmail.com> | 2015-02-03 21:39:47 +0100 |
|---|---|---|
| committer | cenci0 <alchimist94@gmail.com> | 2015-02-03 21:39:47 +0100 |
| commit | 5b77a4eaab418836820b2a0bad7c88f0b88136a0 (patch) | |
| tree | 5728296a2f09148c3818ca727238898a021530bd /static/classes | |
| parent | c359aa20cd0876e1e497aca1ff19e4c54287b6c2 (diff) | |
Minor code refactoring and updates
DATABASE UPDATE: `orginalname` renamed to `originalname` (run update.sql to update `files` table).
Run `update.sql` to update your database, no data will be lost (if you don't update it, pomf won't work properly).
`schema.sql`
- `id`s are now unsigned to double the maximum number of supported indexes
- `size` and `user` columns are unsigned too (we can't have a negative file size or a negative user id)
`static/classes/Response.class.php`
New class to manage responses and errors. Using a class for responses makes the code cleaner and easier to add new response type handlers in the future.
- The response type is declared while a new variable is initialized and the `error` and `send` methods will print a proper output according to it
- The header for the response is also set accordingly to the response type (`text/plain` for `csv`/`gyazo` output, `application/json` for `json` output)
`static/classes/UploadException.class.php`
`Exception` class extension to manage errors that may have occurred while uploading files to the server. This way every error is reported in detail.
`static/includes/settings.inc.php`
- `POMF_URL`: add an option to set the domain of the url to prepend to the newly generated filename (before it was hardcoded and it was `http://a.pomf.se/`) [this option is currently used only when the output is set to `gyazo`]
`upload.php`
- Add functions calls at the beginning to enable the output compression if possible (gzipped output)
- Include new classes
- `generate_name ()`
--- The new name generation algorithm is basically the same of the old one, but it's easier to add/remove characters if needed (eg. lower-case letters isn't enough and you need to add capital-case letters or digits)
--- Check directly the database instead of the filesystem to see if a generated filename is already in use
- `upload_file ()`
--- File errors generate a `UploadException` instead of a normal `Exception`
--- Move common code outside the `if` to avoid useless code repetition (only the query is now inside the condition)
--- Change file permissions of a file after moving it to the new directory (if the user didn't set a mask previouly these files wouldn't be world readable, we make sure they are)
- `diverse_array ()`
--- Function to reorder initial `$_FILES` array (easier to handle for `refiles`)
- `refiles ()`
--- Use a `foreach` cicle instead of a `for` one, easier to understand
- Create a new response object and set its response type (`csv`, `gyazo` or default to `json`)
- Edit response handling according to the new response class
Diffstat (limited to 'static/classes')
| -rwxr-xr-x | static/classes/Response.class.php | 98 | ||||
| -rwxr-xr-x | static/classes/UploadException.class.php | 45 | ||||
| -rw-r--r-- | static/classes/UploadedFile.class.php | 13 |
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; } |
