mirror of
https://github.com/strapi/strapi.git
synced 2025-07-23 17:10:08 +00:00
Merge pull request #13996 from strapi/fix/upload-image-without-extension
Fix: upload media image with non standard urls
This commit is contained in:
commit
21d06ce7d0
@ -161,7 +161,7 @@ describe('UploadAssetDialog', () => {
|
||||
|
||||
const assets = [
|
||||
{
|
||||
name: 'http://localhost:5000/an-image.png',
|
||||
name: 'an-image.png',
|
||||
ext: 'png',
|
||||
mime: 'image/png',
|
||||
source: 'url',
|
||||
@ -170,7 +170,7 @@ describe('UploadAssetDialog', () => {
|
||||
rawFile: new File([''], 'image/png'),
|
||||
},
|
||||
{
|
||||
name: 'http://localhost:5000/a-pdf.pdf',
|
||||
name: 'a-pdf.pdf',
|
||||
ext: 'pdf',
|
||||
mime: 'application/pdf',
|
||||
source: 'url',
|
||||
@ -179,7 +179,7 @@ describe('UploadAssetDialog', () => {
|
||||
rawFile: new File([''], 'application/pdf'),
|
||||
},
|
||||
{
|
||||
name: 'http://localhost:5000/a-video.mp4',
|
||||
name: 'a-video.mp4',
|
||||
ext: 'mp4',
|
||||
mime: 'video/mp4',
|
||||
source: 'url',
|
||||
@ -188,7 +188,7 @@ describe('UploadAssetDialog', () => {
|
||||
rawFile: new File([''], 'video/mp4'),
|
||||
},
|
||||
{
|
||||
name: 'http://localhost:5000/not-working-like-cors.lutin',
|
||||
name: 'not-working-like-cors.lutin',
|
||||
ext: 'lutin',
|
||||
mime: 'application/json',
|
||||
source: 'url',
|
||||
|
@ -2,6 +2,10 @@ import axios from 'axios';
|
||||
import { AssetSource } from '../constants';
|
||||
import { typeFromMime } from './typeFromMime';
|
||||
|
||||
function getFilenameFromURL(url) {
|
||||
return new URL(url).pathname.split('/').pop();
|
||||
}
|
||||
|
||||
export const urlsToAssets = async (urls) => {
|
||||
const assetPromises = urls.map((url) =>
|
||||
axios
|
||||
@ -10,7 +14,7 @@ export const urlsToAssets = async (urls) => {
|
||||
timeout: 60000,
|
||||
})
|
||||
.then((res) => {
|
||||
const loadedFile = new File([res.data], res.config.url, {
|
||||
const loadedFile = new File([res.data], getFilenameFromURL(res.config.url), {
|
||||
type: res.headers['content-type'],
|
||||
});
|
||||
|
||||
|
@ -12,6 +12,7 @@ const crypto = require('crypto');
|
||||
const fs = require('fs');
|
||||
const fse = require('fs-extra');
|
||||
const _ = require('lodash');
|
||||
const { extension } = require('mime-types');
|
||||
const {
|
||||
sanitize,
|
||||
nameToSlug,
|
||||
@ -72,7 +73,10 @@ module.exports = ({ strapi }) => ({
|
||||
async formatFileInfo({ filename, type, size }, fileInfo = {}, metas = {}) {
|
||||
const fileService = getService('file');
|
||||
|
||||
const ext = path.extname(filename);
|
||||
let ext = path.extname(filename);
|
||||
if (!ext) {
|
||||
ext = `.${extension(type)}`;
|
||||
}
|
||||
const basename = path.basename(fileInfo.name || filename, ext);
|
||||
const usedName = fileInfo.name || filename;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user