<?php

class DownloadController {
	public static function get_download($id) {
		return new DownloadJob($id);
	}

	public static function start_download() {
	}

	public static function api_downloads() {
		$s = DownloadJob::find([["processed", "=", 0], ["owner", "=", get_user()->id]])->orderBy("queued")->limit(20);
		return $s->all();
	}

	public static function api_add_download($_request) {
		$url = $_request->put("url");
		$d = new DownloadJob;
		$d->url = $url;
		$d->queued = time();
		$d->started = 0;
		$d->finished = 0;
		$d->processed = 0;
		$d->owner = get_user()->id;
        $d->file = sprintf("download/file-%08X-%08X", rand(), time());

		$d->save();
		return DownloadController::api_downloads();
	}

}
