<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class files extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'files';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $url = "https://flibusta.site/b/354092/fb2";
        echo "start\n";

        $id = "354092";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
        curl_setopt($ch, CURLOPT_URL, $url);
        $data = curl_exec($ch);
        curl_close($ch);

        file_put_contents("/var/www/html/booksbot/storage/tmp_files/{$id}.zip",$data);
        exec("unzip /var/www/html/booksbot/storage/tmp_files/{$id}.zip -d /var/www/html/booksbot/storage/tmp_files");
        sleep(1);
        exec("ebook-convert '/var/www/html/booksbot/storage/tmp_files/{$id}.fb2' '/var/www/html/booksbot/storage/tmp_files/{$id}.epub'");

    }




    public function getElementValue($string,$strStart,$strEnd){
        $str_search = strpos($string,$strStart);
        if($str_search===false){
            return false;
        }
        $str_1 = substr($string,$str_search+strlen($strStart));
        $str_search = strpos($str_1,$strEnd);
        $res = substr($str_1,0,$str_search);
        return $res;
    }

}
