mirror of
https://github.com/strapi/strapi.git
synced 2025-07-25 09:56:53 +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 = [
|
const assets = [
|
||||||
{
|
{
|
||||||
name: 'http://localhost:5000/an-image.png',
|
name: 'an-image.png',
|
||||||
ext: 'png',
|
ext: 'png',
|
||||||
mime: 'image/png',
|
mime: 'image/png',
|
||||||
source: 'url',
|
source: 'url',
|
||||||
@ -170,7 +170,7 @@ describe('UploadAssetDialog', () => {
|
|||||||
rawFile: new File([''], 'image/png'),
|
rawFile: new File([''], 'image/png'),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'http://localhost:5000/a-pdf.pdf',
|
name: 'a-pdf.pdf',
|
||||||
ext: 'pdf',
|
ext: 'pdf',
|
||||||
mime: 'application/pdf',
|
mime: 'application/pdf',
|
||||||
source: 'url',
|
source: 'url',
|
||||||
@ -179,7 +179,7 @@ describe('UploadAssetDialog', () => {
|
|||||||
rawFile: new File([''], 'application/pdf'),
|
rawFile: new File([''], 'application/pdf'),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'http://localhost:5000/a-video.mp4',
|
name: 'a-video.mp4',
|
||||||
ext: 'mp4',
|
ext: 'mp4',
|
||||||
mime: 'video/mp4',
|
mime: 'video/mp4',
|
||||||
source: 'url',
|
source: 'url',
|
||||||
@ -188,7 +188,7 @@ describe('UploadAssetDialog', () => {
|
|||||||
rawFile: new File([''], 'video/mp4'),
|
rawFile: new File([''], 'video/mp4'),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'http://localhost:5000/not-working-like-cors.lutin',
|
name: 'not-working-like-cors.lutin',
|
||||||
ext: 'lutin',
|
ext: 'lutin',
|
||||||
mime: 'application/json',
|
mime: 'application/json',
|
||||||
source: 'url',
|
source: 'url',
|
||||||
|
@ -2,6 +2,10 @@ import axios from 'axios';
|
|||||||
import { AssetSource } from '../constants';
|
import { AssetSource } from '../constants';
|
||||||
import { typeFromMime } from './typeFromMime';
|
import { typeFromMime } from './typeFromMime';
|
||||||
|
|
||||||
|
function getFilenameFromURL(url) {
|
||||||
|
return new URL(url).pathname.split('/').pop();
|
||||||
|
}
|
||||||
|
|
||||||
export const urlsToAssets = async (urls) => {
|
export const urlsToAssets = async (urls) => {
|
||||||
const assetPromises = urls.map((url) =>
|
const assetPromises = urls.map((url) =>
|
||||||
axios
|
axios
|
||||||
@ -10,7 +14,7 @@ export const urlsToAssets = async (urls) => {
|
|||||||
timeout: 60000,
|
timeout: 60000,
|
||||||
})
|
})
|
||||||
.then((res) => {
|
.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'],
|
type: res.headers['content-type'],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -12,6 +12,7 @@ const crypto = require('crypto');
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const fse = require('fs-extra');
|
const fse = require('fs-extra');
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
const { extension } = require('mime-types');
|
||||||
const {
|
const {
|
||||||
sanitize,
|
sanitize,
|
||||||
nameToSlug,
|
nameToSlug,
|
||||||
@ -72,7 +73,10 @@ module.exports = ({ strapi }) => ({
|
|||||||
async formatFileInfo({ filename, type, size }, fileInfo = {}, metas = {}) {
|
async formatFileInfo({ filename, type, size }, fileInfo = {}, metas = {}) {
|
||||||
const fileService = getService('file');
|
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 basename = path.basename(fileInfo.name || filename, ext);
|
||||||
const usedName = fileInfo.name || filename;
|
const usedName = fileInfo.name || filename;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user