Description

Recently Dropbox starts refusing requests with empty headers and body, this mod will help you fix “Couldn’t get file type” issue.

Changelog

Version 1.0 - 7.06.2020
- Initial release.

Instruction

Suggestion: use Microsoft Visual Studio Code for code editing and profiling. All your syntax error will be highlighted.

Before we start making changes to the code, I strongly recommend you to backup the source files so that you do not have any problems if something goes wrong. 

The list of files that we will change:

/app/lib/OneFileManager/FileGrabber.php

Well, let’s get started. 

Step 1

Open file /app/lib/OneFileManager/FileGrabber.php and find function private function grabFromUrl():

Replace all function code with new one:

/**
     * Grab a file from general URL
     * @return [type] [description]
     */
    private function grabFromUrl()
    {
        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_HEADER => true,
            CURLOPT_NOBODY => true,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_URL => $this->url
        ]);
        $output = @curl_exec($curl); 
        curl_close($curl);

        if (empty($output)) {
            return Common::error(__("Couldn't download the image! Check the URL."));
        }

        $headers = [];
        $output = rtrim($output);
        $data = explode("\n",$output);
        $headers['status'] = $data[0];
        array_shift($data);
        foreach($data as $part){
            $middle = explode(":",$part,2);
            if (!isset($middle[1])) {
                $middle[1] = null;
            }
            $headers[trim($middle[0])] = trim($middle[1]);
        }

        if (!$headers) {
            return Common::error(__("Couldn't find the image!"));
        }

        if (empty($headers["Content-Type"])) {
            return print_r($headers);
            return Common::error(__("Couldn't get file type!"));
        }

        $mime = $headers["Content-Type"];
        if (is_array($mime)) {
            $mime = array_pop($mime);
        }
        $ext = trim(Common::mimeToExt($mime), ".");

        try {
            $this->console->validateFileExt($ext);
        } catch (\Exception $e) {
            return Common::error($e->getMessage());
        }


        $filename = uniqid(readableRandomString(8)."-").".".$ext;
        $downres = file_put_contents($this->manager->getOption("path") . $filename, @file_get_contents($this->url));

        if (!$downres) {
            return Common::error("Couldn't download the file");
        }

        // Check file size
        $filesize = filesize($this->manager->getOption("path") . $filename);
        try {
            $this->console->validateFileSize($filesize);
        } catch (\Exception $e) {
            return Common::error($e->getMessage());
        }


        // Process the media
        $filename = $this->console->processMedia($filename);
        // File type might be changed, 
        // Get file extension again
        $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));

        $file = $this->console->addDBRecord([
            "title" => basename($this->url),
            "filename" => $filename,
            "filesize" => $filesize
        ]);

        if (!$file) {
            unlink($this->manager->getOption("path") . $filename);
            return Common::error(__("Couldn't save uploaded file data."));
        }

        $this->console->outdata->file = [
            "id" => $file->id,
            "title" => $file->title,
            "info" => $file->info,
            "filename" => $file->filename,
            "filesize" => $file->filesize,
            "ext" => $ext,
            "url" => $this->manager->getOption("url") . $file->filename,
            "date" => $file->date,
            "icon" => Common::isVideo($file->filename) ? "mdi mdi-play" : false
        ];

        $this->console->outdata->max_storage_size = $this->manager->getOption("max_storage_size");
        $this->console->outdata->used_storage_size = $this->console->getUsedStorageSize();

        return Common::success($this->console->outdata);
    }

That’s all, thank you!

Now clean browser cache and take a look at your changes. For example I attached screenshot from Chrome.

This image has an empty alt attribute; its file name is image-1024x261.png