diff --git a/src/actions/mediaLibrary.js b/src/actions/mediaLibrary.js index 4ba512a53e3f..2af3386779f9 100644 --- a/src/actions/mediaLibrary.js +++ b/src/actions/mediaLibrary.js @@ -4,6 +4,7 @@ import { createAssetProxy } from 'ValueObjects/AssetProxy'; import { getAsset, selectIntegration } from 'Reducers'; import { getIntegrationProvider } from 'Integrations'; import { addAsset } from './media'; +import { sanitizeSlug } from "Lib/urlHelper"; const { notifSend } = notifActions; @@ -79,7 +80,8 @@ export function persistMedia(file, opts = {}) { const backend = currentBackend(state.config); const integration = selectIntegration(state, null, 'assetStore'); const files = state.mediaLibrary.get('files'); - const existingFile = files.find(existingFile => existingFile.name.toLowerCase() === file.name.toLowerCase()); + const fileName = sanitizeSlug(file.name.toLowerCase(), { slugType: state.config.get('slug_type') }); + const existingFile = files.find(existingFile => existingFile.name.toLowerCase() === fileName); /** * Check for existing files of the same name before persisting. If no asset @@ -98,7 +100,7 @@ export function persistMedia(file, opts = {}) { dispatch(mediaPersisting()); try { - const assetProxy = await createAssetProxy(file.name.toLowerCase(), file, false, privateUpload); + const assetProxy = await createAssetProxy(fileName, file, false, privateUpload); dispatch(addAsset(assetProxy)); if (!integration) { const asset = await backend.persistMedia(assetProxy); diff --git a/src/backends/backend.js b/src/backends/backend.js index 63ac5e5dc5ed..6545432cb6bb 100644 --- a/src/backends/backend.js +++ b/src/backends/backend.js @@ -11,7 +11,6 @@ import { } from "Reducers/collections"; import { createEntry } from "ValueObjects/Entry"; import { sanitizeSlug } from "Lib/urlHelper"; -import diacritics from 'diacritics'; import TestRepoBackend from "./test-repo/implementation"; import GitHubBackend from "./github/implementation"; import GitGatewayBackend from "./git-gateway/implementation"; @@ -80,11 +79,6 @@ const slugFormatter = (template = "{{slug}}", entryData, slugType) => { // Replace periods with dashes. .replace(/[.]/g, '-'); - if (slugType === "latin") { - const latinSlug = diacritics.remove(slug); - return sanitizeSlug(latinSlug, { slugType: "ascii" }); - } - return sanitizeSlug(slug, { slugType }); }; diff --git a/src/lib/urlHelper.js b/src/lib/urlHelper.js index fd41c9a90cdb..a13d555f6e5d 100644 --- a/src/lib/urlHelper.js +++ b/src/lib/urlHelper.js @@ -1,4 +1,5 @@ import url from 'url'; +import diacritics from 'diacritics'; import sanitizeFilename from 'sanitize-filename'; import { isString, escapeRegExp, flow, partialRight } from 'lodash'; @@ -61,12 +62,23 @@ export function sanitizeURI(str, { replacement = "", type = "iri" } = {}) { export function sanitizeSlug(str, { replacement = '-', slugType } = {}) { if (!isString(str)) throw "The input slug must be a string."; if (!isString(replacement)) throw "`options.replacement` must be a string."; - + // Sanitize as URI and as filename. - const sanitize = flow([ + let sanitize = flow([ partialRight(sanitizeURI, { replacement, type: slugType }), partialRight(sanitizeFilename, { replacement }), ]); + + // For `latin` slug type, strip diacritics and use ASCII URL. + if (slugType === "latin") { + sanitize = flow([ + diacritics.remove, + partialRight(sanitizeURI, { replacement, type: 'ascii' }), + partialRight(sanitizeFilename, { replacement }), + ]); + } + + // Run sanitizers. const sanitizedSlug = sanitize(str); // Remove any doubled or trailing replacement characters (that were added in the sanitizers).