<?php

namespace app\admin\controller;

use app\model\Admins;
use app\model\Uploads;
use app\model\Backs;
use app\model\Finances;
use app\model\Onlines;
use app\model\Orders;
use DateTime;
use Qiniu\Auth;
use support\Log;
use support\Redis;
use support\Request;

class UploadController extends base
{
    public function index(Request $request)
    {
        $file = $request->file("file");

        if (!$file->isValid()) {
            return $this->error(400, 'upload fail, code=' . $file->getUploadErrorCode());
        }

        $now = date_create();
        $savepath = sprintf("/uploads/%d/%s/%s", $request->admin->id, $now->format("YmdHisu"), $file->getUploadName());
        $item = new Uploads();
        $item->admin_id = $request->admin->id;
        $item->filesize = 0;
        $item->filepath = $savepath;
        $item->mime = $file->getUploadMimeType();
        $item->create_at = $now->getTimestamp();

        $file->move(public_path(). $savepath);
        $item->save();

        return $this->success($savepath);
    }
}