<?php

/*
 * Handles all uploads
 */

class Upload {

    // Acceptable filetypes
    private $filetypes = ['jpeg', 'jpg', 'png', 'svg', 'gif'];

    private function __construct() {
        
    }

    public static function handleUpload($file = false) {
        $errors = [];
        $filename = $file['name'];
        $size = $file['size'];
        $tempname = $file['tempname'];
        $type = $file['type'];
        $extension = strtolower(end(explode('.', $filename)));

        if (!in_array($extension, $this->filetypes)) {
            // Error! This file does not have an acceptable filetype
            $errors[] = "Filtype ikke accepteret.";
        }

        if ($size > 5000000) {
            // We accept a max size of 5 megabytes. (not 5 mebibytes)
            $errors[] = "Fil for stor.";
        }

        if (empty($errors)) {
            // Do fileupload
            
            $makeUpload = move_uploaded_file($tempname, Config::$file_path);
            
            if (!$makeUpload) {
                $errors[] = "Der opstod en fejl under upload af billedet.";
            } else {
                return true;
            }
            
        }
    }

}
