public function send_file()

in lib.php [1141:1221]


    public function send_file($storedfile, $lifetime = null , $filter = 0, $forcedownload = false, array $options = null) {
        global $USER;
        $caller = '\repository_office365::send_file';
        $reference = $this->unpack_reference($storedfile->get_reference());

        $fileuserid = $storedfile->get_userid();

        if (!isset($reference['source'])) {
            utils::debug('File reference is broken - no source parameter.', 'send_file', $reference);
            send_file_not_found();
            die();
        }

        $doembed = $this->do_embedding($reference, $forcedownload);

        switch ($reference['source']) {
            case 'onedrive':
                $sourceclient = $this->get_onedrive_apiclient(false, $fileuserid);
                if (empty($sourceclient)) {
                    utils::debug('Could not construct api client for user', 'send_file', $fileuserid);
                    send_file_not_found();
                    die();
                }
                if ($doembed === true) {
                    $fileinfo = $sourceclient->get_file_metadata($reference['id']);
                    if (isset($fileinfo['webUrl'])) {
                        $fileurl = $fileinfo['webUrl'];
                    } else {
                        $fileurl = (isset($reference['url'])) ? $reference['url'] : '';
                    }
                } else {
                    $fileurl = (isset($reference['url'])) ? $reference['url'] : '';
                }
                break;

            case 'onedrivegroup':
                $sourceclient = $this->get_unified_apiclient();
                $fileurl = (isset($reference['url'])) ? $reference['url'] : '';
                break;

            default:
                utils::debug('File reference is broken - invalid source parameter.', 'send_file', $reference);
                send_file_not_found();
                die();
        }

        // Do embedding if relevant.
        if ($doembed === true) {
            if (utils::is_o365_connected($USER->id) !== true) {
                // Embedding currently only supported for logged-in Microsoft 365 users.
                echo get_string('erroro365required', 'repository_office365');
                die();
            }
            if (!empty($sourceclient)) {
                if (empty($fileurl)) {
                    $errstr = 'Embed was requested, but could not get file info to complete request.';
                    utils::debug($errstr, 'send_file', ['reference' => $reference, 'fileinfo' => $fileinfo]);
                } else {
                    try {
                        $embedurl = $sourceclient->get_embed_url($reference['id'], $fileurl);
                        $embedurl = (isset($embedurl['value'])) ? $embedurl['value'] : '';
                    } catch (\Exception $e) {
                        // Note: exceptions will already be logged in get_embed_url.
                        $embedurl = '';
                    }
                    if (!empty($embedurl)) {
                        redirect($embedurl);
                    } else if (!empty($fileurl)) {
                        redirect($fileurl);
                    } else {
                        $errstr = 'Embed was requested, but could not complete.';
                        utils::debug($errstr, 'send_file', $reference);
                    }
                }
            } else {
                utils::debug('Could not construct OneDrive client for system api user.', 'send_file');
            }
        }

        redirect($fileurl);
    }