Skip to content

Commit

Permalink
Slugify media files.
Browse files Browse the repository at this point in the history
  • Loading branch information
tech4him1 committed Feb 24, 2018
1 parent 50efaae commit b81abe5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/actions/mediaLibrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down
6 changes: 0 additions & 6 deletions src/backends/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 });
};

Expand Down
16 changes: 14 additions & 2 deletions src/lib/urlHelper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import url from 'url';
import diacritics from 'diacritics';
import sanitizeFilename from 'sanitize-filename';
import { isString, escapeRegExp, flow, partialRight } from 'lodash';

Expand Down Expand Up @@ -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).
Expand Down

0 comments on commit b81abe5

Please sign in to comment.