Skip to content

Commit f313073

Browse files
authored
Merge pull request #2809 from Armanul46/fixed/image-import-issue
Fix: CSV import fails to download images from extension-less CDN URLs (e.g. logo.dev)
2 parents e372e68 + 1e556f5 commit f313073

1 file changed

Lines changed: 29 additions & 7 deletions

File tree

includes/rest-api/functions.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,35 @@ function directorist_rest_upload_image_from_url( $image_url ) {
126126

127127
$allowed_mime_types = get_allowed_mime_types();
128128

129-
// Add extension to the name when downloaded from extension less url
130-
if ( strrpos( $file_array['name'], '.' ) === false ) {
131-
$mime_type = mime_content_type( $file_array['tmp_name'] );
132-
$_mime_types = array_flip( $allowed_mime_types );
133-
$extensions = isset( $_mime_types[ $mime_type ] ) ? $_mime_types[ $mime_type ] : '';
134-
$extensions = explode( '|', $extensions, 2 );
135-
$file_array['name'] .= '.' . $extensions[0];
129+
// Add extension to the name when downloaded from extension-less URL,
130+
// or when the URL path ends with a domain-like suffix (e.g. .com) that
131+
// is not a valid image extension — as happens with logo.dev URLs.
132+
$name_ext = strtolower( pathinfo( $file_array['name'], PATHINFO_EXTENSION ) );
133+
$_mime_types = array_flip( $allowed_mime_types );
134+
$ext_is_valid = false;
135+
136+
if ( $name_ext ) {
137+
foreach ( $allowed_mime_types as $exts => $mime ) {
138+
if ( in_array( $name_ext, explode( '|', $exts ), true ) ) {
139+
$ext_is_valid = true;
140+
break;
141+
}
142+
}
143+
}
144+
145+
if ( ! $ext_is_valid ) {
146+
$mime_type = mime_content_type( $file_array['tmp_name'] );
147+
$extensions = isset( $_mime_types[ $mime_type ] ) ? $_mime_types[ $mime_type ] : '';
148+
$extensions = explode( '|', $extensions, 2 );
149+
150+
// Strip the invalid extension (e.g. ".com") before appending the real one.
151+
if ( $name_ext ) {
152+
$file_array['name'] = substr( $file_array['name'], 0, strrpos( $file_array['name'], '.' ) );
153+
}
154+
155+
if ( ! empty( $extensions[0] ) ) {
156+
$file_array['name'] .= '.' . $extensions[0];
157+
}
136158
}
137159

138160
// Do the validation and storage stuff.

0 commit comments

Comments
 (0)