Skip to content

Allow for archive downloads without extension #11513

@imme-emosol

Description

@imme-emosol

FileDownloader.php its getFileName seems to extract an extension based on the given url.

Recently I came across an archived package publication to a extensionless file name, e.g. ".../download".

Upon inspecting the curl-headers I saw the response included a Content-Disposition header, which I used to detect the extension, which seems to affect the chosen unarchiving procedure.
Later I thought about that Content-Type might be more widely used/available.

Anyway, perhaps a feature to consider adding into composer.

I temporarily resolved my issue by this replacing getFileName its content with this pile of characters.

With php hilighting (as that seems to get lost in the ```diff):

    protected function getFileName(PackageInterface $package, string $path): string
    {
        $extension = $this->getDistPath($package, PATHINFO_EXTENSION);
        if (!$extension) {
            $context=stream_context_create(['http'=>['method'=>'HEAD',],]);
            $headers = get_headers($package->getDistUrl(),true,$context);
            if ($headers['Content-Disposition']){
                $disposition = explode(';', $headers['Content-Disposition']);
                $disposition = array_map('trim', $disposition);
                foreach ($disposition as $k => $e) {
                    $f=explode('=',$e);
                    unset($disposition[$k]);
                    $disposition[$f[0]]=$f[1]??null;
                }
                if (isset($disposition['filename*'])) {
                    $fileName = str_replace('UTF-8\'\'', '', $disposition['filename*']);
                    $extension = pathinfo($fileName, PATHINFO_EXTENSION);
                }
                elseif (isset($disposition['filename'])) {
                    $fileName = substr($disposition['filename'], 1, -1);
                    $extension = pathinfo($fileName, PATHINFO_EXTENSION);
                }
            }
        }
        return rtrim($this->config->get('vendor-dir') . '/composer/tmp-' . md5($package . spl_object_hash($package)) . '.' . $extension, '.');
    }

The ```diff:

    protected function getFileName(PackageInterface $package, string $path): string
    {
-        return rtrim($this->config->get('vendor-dir') . '/composer/tmp-' . md5($package . spl_object_hash($package)) . '.' . $this->getDistPath($package, PATHINFO_EXTENSION), '.');
+        $extension = $this->getDistPath($package, PATHINFO_EXTENSION);
+        if (!$extension) {
+            $context=stream_context_create(['http'=>['method'=>'HEAD',],]);
+            $headers = get_headers($package->getDistUrl(),true,$context);
+            if ($headers['Content-Disposition']){
+                $disposition = explode(';', $headers['Content-Disposition']);
+                $disposition = array_map('trim', $disposition);
+                foreach ($disposition as $k => $e) {
+                    $f=explode('=',$e);
+                    unset($disposition[$k]);
+                    $disposition[$f[0]]=$f[1]??null;
+                }
+                if (isset($disposition['filename*'])) {
+                    $fileName = str_replace('UTF-8\'\'', '', $disposition['filename*']);
+                    $extension = pathinfo($fileName, PATHINFO_EXTENSION);
+                }
+                elseif (isset($disposition['filename'])) {
+                    $fileName = substr($disposition['filename'], 1, -1);
+                    $extension = pathinfo($fileName, PATHINFO_EXTENSION);
+                }
+            }
+        }
+        return rtrim($this->config->get('vendor-dir') . '/composer/tmp-' . md5($package . spl_object_hash($package)) . '.' . $extension, '.');
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions