<commit_before><?php

namespace OCA\Eudat\Job;

use OCA\Eudat\AppInfo\Application;
use OCA\Eudat\Db\FilecacheStatusMapper;

use OC\BackgroundJob\QueuedJob;
use OC\Files\Filesystem;
use OCP\IConfig;
use OCP\Util;


class TransferHandler extends QueuedJob {

    protected $mapper;
    protected $config;

    public function __construct(FilecacheStatusMapper $mapper = null,
                                IConfig $config = null){
        if ($mapper === null || $config === null) {
            $this->fixTransferForCron();

        }
        else {
            $this->mapper = $mapper;
            $this->config = $config;
        }
    }

    protected function fixTransferForCron() {
        $application = new Application();
        $this->mapper = $application->getContainer()->query('FilecacheStatusMapper');
        $this->config = \OC::$server->getConfig();
    }

    /**
     * Check if current user is the requested user
     * @param \array     args
     * @return \null
     */
    public function run($args){
        if(!array_key_exists('transferId', $args)){
            Util::writeLog('transfer', 'Bad request, can not handle transfer without transferId', 3);
            return;
        }
        // get the file transfer object for current job
        $fcStatus = $this->mapper->find($args['transferId']);

        $fcStatus->setStatus("processing");
        $this->mapper->update($fcStatus);
        $user = $fcStatus->getOwner();
        $fileId = $fcStatus->getFileid();

        Util::writeLog('transfer', 'Publishing to'.$this->config->getAppValue('eudat', 'b2share_endpoint_url'), 3);

        Filesystem::init($user, '/');
        $path = Filesystem::getPath($fileId);
        $has_access = Filesystem::isReadable($path);
        if ($has_access) {
            $view = Filesystem::getView();
            $handle = $view->fopen($path, 'rb');
            if ($handle) {
                $chunkSize = 8192; // 8 kB chunks
                ob_start();
                while (!feof($handle)) {
                    echo fread($handle, $chunkSize);
                    flush();
                }
                $content = ob_get_contents();
                $size = $view->filesize($path);
                #return $size;
            }
            #$length = Filesystem::readfile($path);

            Util::writeLog('transfer_path', 'filelength:' . $size, 3);
            Util::writeLog('transfer_path', 'filecontent:' . $content, 3);
            $fcStatus->setStatus("published");
            $this->mapper->update($fcStatus);
        }
        else {
            $fcStatus->setStatus("error while publishing");
            $this->mapper->update($fcStatus);
        }




        // init assertions
        #if(!function_exists('pcntl_fork')){
        #    Util::writeLog('transfer', 'no function `pcntl_fork` install `pcntl` extension', 3);
        #    die("no function `pcntl_fork` install `pcntl` extension" . PHP_EOL);
        #}
        #if(!function_exists('posix_getpid')){
        #    Util::writeLog('transfer', 'no function `posix_getpid`, this feature works on a posix OS only', 3);
        #    die("no function `posix_getpid`, this feature works on a posix OS only" . PHP_EOL);
        #}
        // print_r($args);

        // TODO: think of a fork alternative or make it possible to not loose the database connection. also it is running only one job per cron run...
        #$pid = \pcntl_fork();
        #if ($pid == -1) {
        #    Util::writeLog('transfer', 'forking error', 3);
        #    die("forking error" . PHP_EOL);
        #}
        #else if($pid) {
        #    Util::writeLog('transfer', 'parent', 3);
        #    return;
        #} else {<commit_msg>third party upload working, tested with openstack swift calls.<commit_after><?php

namespace OCA\Eudat\Job;

use OCA\Eudat\AppInfo\Application;
use OCA\Eudat\Db\FilecacheStatusMapper;

use OC\BackgroundJob\QueuedJob;
use OC\Files\Filesystem;
use OCP\IConfig;
use OCP\Util;


class TransferHandler extends QueuedJob {

    protected $mapper;
    protected $config;

    public function __construct(FilecacheStatusMapper $mapper = null,
                                IConfig $config = null){
        if ($mapper === null || $config === null) {
            $this->fixTransferForCron();

        }
        else {
            $this->mapper = $mapper;
            $this->config = $config;
        }
    }

    protected function fixTransferForCron() {
        $application = new Application();
        $this->mapper = $application->getContainer()->query('FilecacheStatusMapper');
        $this->config = \OC::$server->getConfig();
    }

    /**
     * Check if current user is the requested user
     * @param \array     args
     * @return \null
     */
    public function run($args){
        if(!array_key_exists('transferId', $args)){
            Util::writeLog('transfer', 'Bad request, can not handle transfer without transferId', 3);
            return;
        }
        // get the file transfer object for current job
        $fcStatus = $this->mapper->find($args['transferId']);

        $fcStatus->setStatus("processing");
        $this->mapper->update($fcStatus);
        $user = $fcStatus->getOwner();
        $fileId = $fcStatus->getFileid();
        $b2share_url = $this->config->getAppValue('eudat', 'b2share_endpoint_url');

        Util::writeLog('transfer', 'Publishing to'.$b2share_url, 3);

        Filesystem::init($user, '/');
        $path = Filesystem::getPath($fileId);
        $has_access = Filesystem::isReadable($path);
        if ($has_access) {
            $view = Filesystem::getView();
            // TODO: is it good to take the owncloud fopen?
            $handle = $view->fopen($path, 'rb');
            $size = $view->filesize($path);

            $curl_client = curl_init($b2share_url.'/'.$args['transferId']);

            curl_setopt($curl_client, CURLOPT_INFILE, $handle);
            curl_setopt($curl_client, CURLOPT_INFILESIZE, $size);
            curl_setopt($curl_client, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl_client, CURLOPT_PUT, true);
            curl_setopt($curl_client, CURLOPT_HTTPHEADER, array('X-Auth-Token: ', 'Expect:'));

            $curl_success = curl_exec($curl_client);
            if (!$curl_success){
                Util::writeLog('transfer_path', 'Error communicating with B2SHARE'. $curl_success, 3);
                $fcStatus->setStatus("error while accessing b2share");
                $this->mapper->update($fcStatus);
            }
            Util::writeLog('transfer_path', 'Communication to B2SHARE successfull', 0);
            $fcStatus->setStatus("published");
            $this->mapper->update($fcStatus);
        }
        else {
            $fcStatus->setStatus("internal error while publishing");
            $this->mapper->update($fcStatus);
        }




        // init assertions
        #if(!function_exists('pcntl_fork')){
        #    Util::writeLog('transfer', 'no function `pcntl_fork` install `pcntl` extension', 3);
        #    die("no function `pcntl_fork` install `pcntl` extension" . PHP_EOL);
        #}
        #if(!function_exists('posix_getpid')){
        #    Util::writeLog('transfer', 'no function `posix_getpid`, this feature works on a posix OS only', 3);
        #    die("no function `posix_getpid`, this feature works on a posix OS only" . PHP_EOL);
        #}
        // print_r($args);

        // TODO: think of a fork alternative or make it possible to not loose the database connection. also it is running only one job per cron run...
        #$pid = \pcntl_fork();
        #if ($pid == -1) {
        #    Util::writeLog('transfer', 'forking error', 3);
        #    die("forking error" . PHP_EOL);
        #}
        #else if($pid) {
        #    Util::writeLog('transfer', 'parent', 3);
        #    return;
        #} else {
{'fraction_non_alphanumeric': 0.14407426246185148, 'fraction_numerical': 0.003942014242115971, 'mean_word_length': 2.248657579512598, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6315353', 'n_tokens_mistral': 2367, 'n_tokens_neox': 2264, 'n_words': 514}