import { attempt, isError } from 'lodash'; import { Map } from 'immutable'; import { resolveFormat } from "Formats/formats"; import { selectIntegration } from 'Reducers/integrations'; import { selectListMethod, selectEntrySlug, selectEntryPath, selectAllowNewEntries, selectAllowDeletion, selectFolderEntryExtension } from "Reducers/collections"; import { createEntry } from "ValueObjects/Entry"; import { sanitizeSlug } from "Lib/urlHelper"; import TestRepoBackend from "./test-repo/implementation"; import GitHubBackend from "./github/implementation"; import GitGatewayBackend from "./git-gateway/implementation"; import { registerBackend, getBackend } from 'Lib/registry'; import { EditorialWorkflowError } from "ValueObjects/errors"; /** * Register internal backends */ registerBackend('git-gateway', GitGatewayBackend); registerBackend('github', GitHubBackend); registerBackend('test-repo', TestRepoBackend); class LocalStorageAuthStore { storageKey = "netlify-cms-user"; retrieve() { const data = window.localStorage.getItem(this.storageKey); return data && JSON.parse(data); } store(userData) { window.localStorage.setItem(this.storageKey, JSON.stringify(userData)); } logout() { window.localStorage.removeItem(this.storageKey); } } const validIdentifierFields = ["title", "path"]; const getIdentifierKey = entryData => { const keys = validIdentifierFields.map(field => { return entryData.findKey((_, key) => { return key.toLowerCase().trim() === field; }); }); return keys.find(key => key !== undefined); }; const getIdentifier = entryData => { const key = getIdentifierKey(entryData); const identifier = entryData.get(key); if (identifier === undefined) { throw new Error("Collection must have a field name that is a valid entry identifier"); } return identifier; }; const slugFormatter = (template = "{{slug}}", entryData, slugConfig) => { const date = new Date(); const slug = template.replace(/\{\{([^\}]+)\}\}/g, (_, field) => { switch (field) { case "year": return date.getFullYear(); case "month": return (`0${ date.getMonth() + 1 }`).slice(-2); case "day": return (`0${ date.getDate() }`).slice(-2); case "hour": return (`0${ date.getHours() }`).slice(-2); case "minute": return (`0${ date.getMinutes() }`).slice(-2); case "second": return (`0${ date.getSeconds() }`).slice(-2); case "slug": return getIdentifier(entryData).trim(); default: return entryData.get(field, "").trim(); } }) // Convert slug to lower-case .toLocaleLowerCase() // Replace periods with dashes. .replace(/[.]/g, '-'); return sanitizeSlug(slug, slugConfig); }; const commitMessageTemplates = Map({ create: 'Create {{collection}} “{{slug}}”', update: 'Update {{collection}} “{{slug}}”', delete: 'Delete {{collection}} “{{slug}}”', uploadMedia: 'Upload “{{path}}”', deleteMedia: 'Delete “{{path}}”' }); const commitMessageFormatter = (type, config, { slug, path, collection }) => { const templates = commitMessageTemplates.merge(config.getIn(['backend', 'commit_messages'], Map())); const messageTemplate = templates.get(type); return messageTemplate.replace(/\{\{([^\}]+)\}\}/g, (_, variable) => { switch (variable) { case 'slug': return slug; case 'path': return path; case 'collection': return collection.get('label'); default: console.warn(`Ignoring unknown variable “${ variable }” in commit message template.`); return ''; } }); } class Backend { constructor(implementation, backendName, authStore = null) { this.implementation = implementation; this.backendName = backendName; this.authStore = authStore; if (this.implementation === null) { throw new Error("Cannot instantiate a Backend with no implementation"); } } currentUser() { if (this.user) { return this.user; } const stored = this.authStore && this.authStore.retrieve(); if (stored && stored.backendName === this.backendName) { return Promise.resolve(this.implementation.restoreUser(stored)).then((user) => { const newUser = {...user, backendName: this.backendName}; // return confirmed/rehydrated user object instead of stored this.authStore.store(newUser); return newUser; }); } return Promise.resolve(null); } authComponent() { return this.implementation.authComponent(); } authenticate(credentials) { return this.implementation.authenticate(credentials).then((user) => { const newUser = {...user, backendName: this.backendName}; if (this.authStore) { this.authStore.store(newUser); } return newUser; }); } logout() { return Promise.resolve(this.implementation.logout()).then(() => { if (this.authStore) { this.authStore.logout(); } }); } getToken = () => this.implementation.getToken(); listEntries(collection) { const listMethod = this.implementation[selectListMethod(collection)]; const extension = selectFolderEntryExtension(collection); const collectionFilter = collection.get('filter'); return listMethod.call(this.implementation, collection, extension) .then(loadedEntries => ( loadedEntries.map(loadedEntry => createEntry( collection.get("name"), selectEntrySlug(collection, loadedEntry.file.path), loadedEntry.file.path, { raw: loadedEntry.data || '', label: loadedEntry.file.label } )) )) .then(entries => ( { entries: entries.map(this.entryWithFormat(collection)), } )) // If this collection has a "filter" property, filter entries accordingly .then(loadedCollection => ( { entries: collectionFilter ? this.filterEntries(loadedCollection, collectionFilter) : loadedCollection.entries } )); } getEntry(collection, slug) { return this.implementation.getEntry(collection, slug, selectEntryPath(collection, slug)) .then(loadedEntry => this.entryWithFormat(collection, slug)(createEntry( collection.get("name"), slug, loadedEntry.file.path, { raw: loadedEntry.data, label: loadedEntry.file.label } )) ); } getMedia() { return this.implementation.getMedia(); } entryWithFormat(collectionOrEntity) { return (entry) => { const format = resolveFormat(collectionOrEntity, entry); if (entry && entry.raw !== undefined) { const data = (format && attempt(format.fromFile.bind(format, entry.raw))) || {}; if (isError(data)) console.error(data); return Object.assign(entry, { data: isError(data) ? {} : data }); } return format.fromFile(entry); }; } unpublishedEntries(collections) { return this.implementation.unpublishedEntries() .then(loadedEntries => loadedEntries.filter(entry => entry !== null)) .then(entries => ( entries.map((loadedEntry) => { const entry = createEntry( loadedEntry.metaData.collection, loadedEntry.slug, loadedEntry.file.path, { raw: loadedEntry.data, isModification: loadedEntry.isModification, } ); entry.metaData = loadedEntry.metaData; return entry; }) )) .then(entries => ({ pagination: 0, entries: entries.reduce((acc, entry) => { const collection = collections.get(entry.collection); if (collection) { acc.push(this.entryWithFormat(collection)(entry)); } return acc; }, []), })); } unpublishedEntry(collection, slug) { return this.implementation.unpublishedEntry(collection, slug) .then((loadedEntry) => { const entry = createEntry( "draft", loadedEntry.slug, loadedEntry.file.path, { raw: loadedEntry.data, isModification: loadedEntry.isModification, }); entry.metaData = loadedEntry.metaData; return entry; }) .then(this.entryWithFormat(collection, slug)); } async checkOverwrite(collection, slug, path, entryData) { const identifierKey = getIdentifierKey(entryData); const identifierField = collection.get('fields').find(field => { return field.get('name') === identifierKey; }); const identifierLabel = identifierField.get('label'); const errorMessage = `\ Duplicate filename found. Please ensure the ${identifierLabel} field is unique from other entries \ in the ${collection.get('name')} collection.\ `; const existingEntry = await this.unpublishedEntry(collection, slug).catch(error => { if (error instanceof EditorialWorkflowError && error.notUnderEditorialWorkflow) { return Promise.resolve(false); } return Promise.reject(error); }); if (existingEntry) { throw new Error(errorMessage); } const publishedEntry = await this.implementation.getEntry(collection, slug, path) .then(({ data }) => data); if (publishedEntry) { throw new Error(errorMessage); } } async persistEntry(config, collection, entryDraft, MediaFiles, integrations, options = {}) { const newEntry = entryDraft.getIn(["entry", "newRecord"]) || false; const parsedData = { title: entryDraft.getIn(["entry", "data", "title"], "No Title"), description: entryDraft.getIn(["entry", "data", "description"], "No Description!"), }; const entryData = entryDraft.getIn(["entry", "data"]).toJS(); let entryObj; if (newEntry) { if (!selectAllowNewEntries(collection)) { throw (new Error("Not allowed to create new entries in this collection")); } const entryData = entryDraft.getIn(['entry', 'data']); const slug = slugFormatter(collection.get('slug'), entryData, config.get('slug')); const path = selectEntryPath(collection, slug); await this.checkOverwrite(collection, slug, path, entryData); entryObj = { path, slug, raw: this.entryToRaw(collection, entryDraft.get('entry')), }; } else { const path = entryDraft.getIn(["entry", "path"]); const slug = entryDraft.getIn(["entry", "slug"]); entryObj = { path, slug, raw: this.entryToRaw(collection, entryDraft.get("entry")), }; } const commitMessage = commitMessageFormatter(newEntry ? 'create' : 'update', config, { collection, slug: entryObj.slug, path: entryObj.path }); const mode = config.get("publish_mode"); const collectionName = collection.get("name"); /** * Determine whether an asset store integration is in use. */ const hasAssetStore = integrations && !!selectIntegration(integrations, null, 'assetStore'); const updatedOptions = { ...options, hasAssetStore }; const opts = { newEntry, parsedData, commitMessage, collectionName, mode, ...updatedOptions }; return this.implementation.persistEntry(entryObj, MediaFiles, opts) .then(() => entryObj.slug); } persistMedia(config, file) { const options = { commitMessage: commitMessageFormatter('uploadMedia', config, { path: file.path }), }; return this.implementation.persistMedia(file, options); } deleteEntry(config, collection, slug) { const path = selectEntryPath(collection, slug); if (!selectAllowDeletion(collection)) { throw (new Error("Not allowed to delete entries in this collection")); } const commitMessage = commitMessageFormatter('delete', config, { collection, slug, path }); return this.implementation.deleteFile(path, commitMessage); } deleteMedia(config, path) { const commitMessage = commitMessageFormatter('deleteMedia', config, { path }); return this.implementation.deleteFile(path, commitMessage); } persistUnpublishedEntry(...args) { return this.persistEntry(...args, { unpublished: true }); } updateUnpublishedEntryStatus(collection, slug, newStatus) { return this.implementation.updateUnpublishedEntryStatus(collection, slug, newStatus); } publishUnpublishedEntry(collection, slug) { return this.implementation.publishUnpublishedEntry(collection, slug); } deleteUnpublishedEntry(collection, slug) { return this.implementation.deleteUnpublishedEntry(collection, slug); } entryToRaw(collection, entry) { const format = resolveFormat(collection, entry.toJS()); const fieldsOrder = this.fieldsOrder(collection, entry); return format && format.toFile(entry.get("data").toJS(), fieldsOrder); } fieldsOrder(collection, entry) { const fields = collection.get('fields'); if (fields) { return collection.get('fields').map(f => f.get('name')).toArray(); } const files = collection.get('files'); const file = (files || []).filter(f => f.get("name") === entry.get("slug")).get(0); if (file == null) { throw new Error(`No file found for ${ entry.get("slug") } in ${ collection.get('name') }`); } return file.get('fields').map(f => f.get('name')).toArray(); } filterEntries(collection, filterRule) { return collection.entries.filter(entry => ( entry.data[filterRule.get('field')] === filterRule.get('value') )); } } export function resolveBackend(config) { const name = config.getIn(["backend", "name"]); if (name == null) { throw new Error("No backend defined in configuration"); } const authStore = new LocalStorageAuthStore(); if (!getBackend(name)) { throw new Error(`Backend not found: ${ name }`); } else { return new Backend(getBackend(name).init(config), name, authStore); } } export const currentBackend = (function () { let backend = null; return (config) => { if (backend) { return backend; } if (config.get("backend")) { return backend = resolveBackend(config); } }; }());