<?php namespace App\Http\Controllers;

use League\CommonMark\Node\Block\Document;
use Validator;
use Illuminate\Http\Request;

class FileController extends Controller
{
    public function upload(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'file' => 'required|mimes:txt,log|max:10240',
        ]);

        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 401);
        }

        $user_id = $request->get('user_id');

        if ($file = $request->file('file')) {
            $fileName = (!empty($user_id) ? $user_id : 0) . '_' . time() . '.log';
            $request->file->move(storage_path(date('Y-m-d')), $fileName);
            return response()->json([
                "success" => true,
                "message" => "File successfully uploaded",
                "file" => $file
            ]);

        }
    }
}
