chore: add code formatting and linting (#952)
This commit is contained in:
@ -1,105 +1,119 @@
|
||||
import { localForage, unsentRequest, then, APIError, Cursor } from "netlify-cms-lib-util";
|
||||
import { Base64 } from "js-base64";
|
||||
import { List, Map } from "immutable";
|
||||
import { flow, partial, pick, get, result } from "lodash";
|
||||
import { localForage, unsentRequest, then, APIError, Cursor } from 'netlify-cms-lib-util';
|
||||
import { Base64 } from 'js-base64';
|
||||
import { List, Map } from 'immutable';
|
||||
import { flow, partial, result } from 'lodash';
|
||||
|
||||
export default class API {
|
||||
constructor(config) {
|
||||
this.api_root = config.api_root || "https://gitlab.com/api/v4";
|
||||
this.api_root = config.api_root || 'https://gitlab.com/api/v4';
|
||||
this.token = config.token || false;
|
||||
this.branch = config.branch || "master";
|
||||
this.repo = config.repo || "";
|
||||
this.repoURL = `/projects/${ encodeURIComponent(this.repo) }`;
|
||||
this.branch = config.branch || 'master';
|
||||
this.repo = config.repo || '';
|
||||
this.repoURL = `/projects/${encodeURIComponent(this.repo)}`;
|
||||
}
|
||||
|
||||
withAuthorizationHeaders = req => (
|
||||
unsentRequest.withHeaders(this.token ? { Authorization: `Bearer ${ this.token }` } : {}, req)
|
||||
);
|
||||
withAuthorizationHeaders = req =>
|
||||
unsentRequest.withHeaders(this.token ? { Authorization: `Bearer ${this.token}` } : {}, req);
|
||||
|
||||
buildRequest = req => flow([
|
||||
unsentRequest.withRoot(this.api_root),
|
||||
this.withAuthorizationHeaders,
|
||||
unsentRequest.withTimestamp,
|
||||
])(req);
|
||||
buildRequest = req =>
|
||||
flow([
|
||||
unsentRequest.withRoot(this.api_root),
|
||||
this.withAuthorizationHeaders,
|
||||
unsentRequest.withTimestamp,
|
||||
])(req);
|
||||
|
||||
request = async req => flow([
|
||||
this.buildRequest,
|
||||
unsentRequest.performRequest,
|
||||
p => p.catch(err => Promise.reject(new APIError(err.message, null, "GitLab"))),
|
||||
])(req);
|
||||
request = async req =>
|
||||
flow([
|
||||
this.buildRequest,
|
||||
unsentRequest.performRequest,
|
||||
p => p.catch(err => Promise.reject(new APIError(err.message, null, 'GitLab'))),
|
||||
])(req);
|
||||
|
||||
parseResponse = async (res, { expectingOk=true, expectingFormat=false }) => {
|
||||
const contentType = res.headers.get("Content-Type");
|
||||
const isJSON = contentType === "application/json";
|
||||
parseResponse = async (res, { expectingOk = true, expectingFormat = false }) => {
|
||||
const contentType = res.headers.get('Content-Type');
|
||||
const isJSON = contentType === 'application/json';
|
||||
let body;
|
||||
try {
|
||||
body = await ((expectingFormat === "json" || isJSON) ? res.json() : res.text());
|
||||
body = await (expectingFormat === 'json' || isJSON ? res.json() : res.text());
|
||||
} catch (err) {
|
||||
throw new APIError(err.message, res.status, "GitLab");
|
||||
throw new APIError(err.message, res.status, 'GitLab');
|
||||
}
|
||||
if (expectingOk && !res.ok) {
|
||||
throw new APIError((isJSON && body.message) ? body.message : body, res.status, "GitLab");
|
||||
throw new APIError(isJSON && body.message ? body.message : body, res.status, 'GitLab');
|
||||
}
|
||||
return body;
|
||||
};
|
||||
|
||||
responseToJSON = res => this.parseResponse(res, { expectingFormat: "json" });
|
||||
responseToText = res => this.parseResponse(res, { expectingFormat: "text" });
|
||||
responseToJSON = res => this.parseResponse(res, { expectingFormat: 'json' });
|
||||
responseToText = res => this.parseResponse(res, { expectingFormat: 'text' });
|
||||
requestJSON = req => this.request(req).then(this.responseToJSON);
|
||||
requestText = req => this.request(req).then(this.responseToText);
|
||||
|
||||
user = () => this.requestJSON("/user");
|
||||
user = () => this.requestJSON('/user');
|
||||
|
||||
WRITE_ACCESS = 30;
|
||||
hasWriteAccess = () => this.requestJSON(this.repoURL).then(({ permissions }) => {
|
||||
const { project_access, group_access } = permissions;
|
||||
if (project_access && (project_access.access_level >= this.WRITE_ACCESS)) {
|
||||
return true;
|
||||
}
|
||||
if (group_access && (group_access.access_level >= this.WRITE_ACCESS)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
readFile = async (path, sha, ref=this.branch) => {
|
||||
const cachedFile = sha ? await localForage.getItem(`gl.${ sha }`) : null;
|
||||
if (cachedFile) { return cachedFile; }
|
||||
const result = await this.requestText({
|
||||
url: `${ this.repoURL }/repository/files/${ encodeURIComponent(path) }/raw`,
|
||||
params: { ref },
|
||||
cache: "no-store",
|
||||
hasWriteAccess = () =>
|
||||
this.requestJSON(this.repoURL).then(({ permissions }) => {
|
||||
const { project_access, group_access } = permissions;
|
||||
if (project_access && project_access.access_level >= this.WRITE_ACCESS) {
|
||||
return true;
|
||||
}
|
||||
if (group_access && group_access.access_level >= this.WRITE_ACCESS) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
if (sha) { localForage.setItem(`gl.${ sha }`, result) }
|
||||
|
||||
readFile = async (path, sha, ref = this.branch) => {
|
||||
const cachedFile = sha ? await localForage.getItem(`gl.${sha}`) : null;
|
||||
if (cachedFile) {
|
||||
return cachedFile;
|
||||
}
|
||||
const result = await this.requestText({
|
||||
url: `${this.repoURL}/repository/files/${encodeURIComponent(path)}/raw`,
|
||||
params: { ref },
|
||||
cache: 'no-store',
|
||||
});
|
||||
if (sha) {
|
||||
localForage.setItem(`gl.${sha}`, result);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
fileDownloadURL = (path, ref=this.branch) => unsentRequest.toURL(this.buildRequest({
|
||||
url: `${ this.repoURL }/repository/files/${ encodeURIComponent(path) }/raw`,
|
||||
params: { ref },
|
||||
}));
|
||||
fileDownloadURL = (path, ref = this.branch) =>
|
||||
unsentRequest.toURL(
|
||||
this.buildRequest({
|
||||
url: `${this.repoURL}/repository/files/${encodeURIComponent(path)}/raw`,
|
||||
params: { ref },
|
||||
}),
|
||||
);
|
||||
|
||||
getCursorFromHeaders = headers => {
|
||||
// indices and page counts are assumed to be zero-based, but the
|
||||
// indices and page counts returned from GitLab are one-based
|
||||
const index = parseInt(headers.get("X-Page"), 10) - 1;
|
||||
const pageCount = parseInt(headers.get("X-Total-Pages"), 10) - 1;
|
||||
const pageSize = parseInt(headers.get("X-Per-Page"), 10);
|
||||
const count = parseInt(headers.get("X-Total"), 10);
|
||||
const linksRaw = headers.get("Link");
|
||||
const links = List(linksRaw.split(","))
|
||||
.map(str => str.trim().split(";"))
|
||||
const index = parseInt(headers.get('X-Page'), 10) - 1;
|
||||
const pageCount = parseInt(headers.get('X-Total-Pages'), 10) - 1;
|
||||
const pageSize = parseInt(headers.get('X-Per-Page'), 10);
|
||||
const count = parseInt(headers.get('X-Total'), 10);
|
||||
const linksRaw = headers.get('Link');
|
||||
const links = List(linksRaw.split(','))
|
||||
.map(str => str.trim().split(';'))
|
||||
.map(([linkStr, keyStr]) => [
|
||||
keyStr.match(/rel="(.*?)"/)[1],
|
||||
unsentRequest.fromURL(linkStr.trim().match(/<(.*?)>/)[1]),
|
||||
])
|
||||
.update(list => Map(list));
|
||||
const actions = links.keySeq().flatMap(key => (
|
||||
(key === "prev" && index > 0) ||
|
||||
(key === "next" && index < pageCount) ||
|
||||
(key === "first" && index > 0) ||
|
||||
(key === "last" && index < pageCount)
|
||||
) ? [key] : []);
|
||||
const actions = links
|
||||
.keySeq()
|
||||
.flatMap(
|
||||
key =>
|
||||
(key === 'prev' && index > 0) ||
|
||||
(key === 'next' && index < pageCount) ||
|
||||
(key === 'first' && index > 0) ||
|
||||
(key === 'last' && index < pageCount)
|
||||
? [key]
|
||||
: [],
|
||||
);
|
||||
return Cursor.create({
|
||||
actions,
|
||||
meta: { index, count, pageSize, pageCount },
|
||||
@ -111,36 +125,42 @@ export default class API {
|
||||
|
||||
// Gets a cursor without retrieving the entries by using a HEAD
|
||||
// request
|
||||
fetchCursor = req => flow([unsentRequest.withMethod("HEAD"), this.request, then(this.getCursor)])(req);
|
||||
fetchCursorAndEntries = req => flow([
|
||||
unsentRequest.withMethod("GET"),
|
||||
this.request,
|
||||
p => Promise.all([p.then(this.getCursor), p.then(this.responseToJSON)]),
|
||||
then(([cursor, entries]) => ({ cursor, entries })),
|
||||
])(req);
|
||||
fetchCursor = req =>
|
||||
flow([unsentRequest.withMethod('HEAD'), this.request, then(this.getCursor)])(req);
|
||||
fetchCursorAndEntries = req =>
|
||||
flow([
|
||||
unsentRequest.withMethod('GET'),
|
||||
this.request,
|
||||
p => Promise.all([p.then(this.getCursor), p.then(this.responseToJSON)]),
|
||||
then(([cursor, entries]) => ({ cursor, entries })),
|
||||
])(req);
|
||||
fetchRelativeCursor = async (cursor, action) => this.fetchCursor(cursor.data.links[action]);
|
||||
|
||||
reversableActions = Map({
|
||||
first: "last",
|
||||
last: "first",
|
||||
next: "prev",
|
||||
prev: "next",
|
||||
first: 'last',
|
||||
last: 'first',
|
||||
next: 'prev',
|
||||
prev: 'next',
|
||||
});
|
||||
|
||||
reverseCursor = cursor => {
|
||||
const pageCount = cursor.meta.get("pageCount", 0);
|
||||
const currentIndex = cursor.meta.get("index", 0);
|
||||
const pageCount = cursor.meta.get('pageCount', 0);
|
||||
const currentIndex = cursor.meta.get('index', 0);
|
||||
const newIndex = pageCount - currentIndex;
|
||||
|
||||
const links = cursor.data.get("links", Map());
|
||||
const links = cursor.data.get('links', Map());
|
||||
const reversedLinks = links.mapEntries(([k, v]) => [this.reversableActions.get(k) || k, v]);
|
||||
|
||||
const reversedActions = cursor.actions.map(action => this.reversableActions.get(action) || action);
|
||||
const reversedActions = cursor.actions.map(
|
||||
action => this.reversableActions.get(action) || action,
|
||||
);
|
||||
|
||||
return cursor.updateStore(store => store
|
||||
.setIn(["meta", "index"], newIndex)
|
||||
.setIn(["data", "links"], reversedLinks)
|
||||
.set("actions", reversedActions));
|
||||
return cursor.updateStore(store =>
|
||||
store
|
||||
.setIn(['meta', 'index'], newIndex)
|
||||
.setIn(['data', 'links'], reversedLinks)
|
||||
.set('actions', reversedActions),
|
||||
);
|
||||
};
|
||||
|
||||
// The exported listFiles and traverseCursor reverse the direction
|
||||
@ -151,16 +171,19 @@ export default class API {
|
||||
// refactored.
|
||||
listFiles = async path => {
|
||||
const firstPageCursor = await this.fetchCursor({
|
||||
url: `${ this.repoURL }/repository/tree`,
|
||||
url: `${this.repoURL}/repository/tree`,
|
||||
params: { path, ref: this.branch },
|
||||
});
|
||||
const lastPageLink = firstPageCursor.data.getIn(["links", "last"]);
|
||||
const lastPageLink = firstPageCursor.data.getIn(['links', 'last']);
|
||||
const { entries, cursor } = await this.fetchCursorAndEntries(lastPageLink);
|
||||
return { files: entries.filter(({ type }) => type === "blob").reverse(), cursor: this.reverseCursor(cursor) };
|
||||
return {
|
||||
files: entries.filter(({ type }) => type === 'blob').reverse(),
|
||||
cursor: this.reverseCursor(cursor),
|
||||
};
|
||||
};
|
||||
|
||||
traverseCursor = async (cursor, action) => {
|
||||
const link = cursor.data.getIn(["links", action]);
|
||||
const link = cursor.data.getIn(['links', action]);
|
||||
const { entries, cursor: newCursor } = await this.fetchCursorAndEntries(link);
|
||||
return { entries: entries.reverse(), cursor: this.reverseCursor(newCursor) };
|
||||
};
|
||||
@ -168,28 +191,31 @@ export default class API {
|
||||
listAllFiles = async path => {
|
||||
const entries = [];
|
||||
let { cursor, entries: initialEntries } = await this.fetchCursorAndEntries({
|
||||
url: `${ this.repoURL }/repository/tree`,
|
||||
url: `${this.repoURL}/repository/tree`,
|
||||
// Get the maximum number of entries per page
|
||||
params: { path, ref: this.branch, per_page: 100 },
|
||||
});
|
||||
entries.push(...initialEntries);
|
||||
while (cursor && cursor.actions.has("next")) {
|
||||
const link = cursor.data.getIn(["links", "next"]);
|
||||
while (cursor && cursor.actions.has('next')) {
|
||||
const link = cursor.data.getIn(['links', 'next']);
|
||||
const { cursor: newCursor, entries: newEntries } = await this.fetchCursorAndEntries(link);
|
||||
entries.push(...newEntries);
|
||||
cursor = newCursor;
|
||||
}
|
||||
return entries.filter(({ type }) => type === "blob");
|
||||
return entries.filter(({ type }) => type === 'blob');
|
||||
};
|
||||
|
||||
toBase64 = str => Promise.resolve(Base64.encode(str));
|
||||
fromBase64 = str => Base64.decode(str);
|
||||
uploadAndCommit = async (item, { commitMessage, updateFile = false, branch = this.branch, author = this.commitAuthor }) => {
|
||||
uploadAndCommit = async (
|
||||
item,
|
||||
{ commitMessage, updateFile = false, branch = this.branch, author = this.commitAuthor },
|
||||
) => {
|
||||
const content = await result(item, 'toBase64', partial(this.toBase64, item.raw));
|
||||
const file_path = item.path.replace(/^\//, "");
|
||||
const action = (updateFile ? "update" : "create");
|
||||
const encoding = "base64";
|
||||
|
||||
const file_path = item.path.replace(/^\//, '');
|
||||
const action = updateFile ? 'update' : 'create';
|
||||
const encoding = 'base64';
|
||||
|
||||
const commitParams = {
|
||||
branch,
|
||||
commit_message: commitMessage,
|
||||
@ -202,9 +228,9 @@ export default class API {
|
||||
}
|
||||
|
||||
await this.request({
|
||||
url: `${ this.repoURL }/repository/commits`,
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
url: `${this.repoURL}/repository/commits`,
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(commitParams),
|
||||
});
|
||||
|
||||
@ -212,7 +238,11 @@ export default class API {
|
||||
};
|
||||
|
||||
persistFiles = (files, { commitMessage, newEntry }) =>
|
||||
Promise.all(files.map(file => this.uploadAndCommit(file, { commitMessage, updateFile: newEntry === false })));
|
||||
Promise.all(
|
||||
files.map(file =>
|
||||
this.uploadAndCommit(file, { commitMessage, updateFile: newEntry === false }),
|
||||
),
|
||||
);
|
||||
|
||||
deleteFile = (path, commit_message, options = {}) => {
|
||||
const branch = options.branch || this.branch;
|
||||
@ -223,10 +253,10 @@ export default class API {
|
||||
commitParams.author_email = email;
|
||||
}
|
||||
return flow([
|
||||
unsentRequest.withMethod("DELETE"),
|
||||
unsentRequest.withMethod('DELETE'),
|
||||
// TODO: only send author params if they are defined.
|
||||
unsentRequest.withParams(commitParams),
|
||||
this.request,
|
||||
])(`${ this.repoURL }/repository/files/${ encodeURIComponent(path) }`);
|
||||
])(`${this.repoURL}/repository/files/${encodeURIComponent(path)}`);
|
||||
};
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ import { AuthenticationPage, Icon } from 'netlify-cms-ui-default';
|
||||
|
||||
const LoginButtonIcon = styled(Icon)`
|
||||
margin-right: 18px;
|
||||
`
|
||||
`;
|
||||
|
||||
export default class GitLabAuthenticationPage extends React.Component {
|
||||
static propTypes = {
|
||||
@ -18,9 +18,9 @@ export default class GitLabAuthenticationPage extends React.Component {
|
||||
|
||||
componentDidMount() {
|
||||
const authType = this.props.config.getIn(['backend', 'auth_type']);
|
||||
if (authType === "implicit") {
|
||||
if (authType === 'implicit') {
|
||||
this.auth = new ImplicitAuthenticator({
|
||||
base_url: this.props.config.getIn(['backend', 'base_url'], "https://gitlab.com"),
|
||||
base_url: this.props.config.getIn(['backend', 'base_url'], 'https://gitlab.com'),
|
||||
auth_endpoint: this.props.config.getIn(['backend', 'auth_endpoint'], 'oauth/authorize'),
|
||||
app_id: this.props.config.getIn(['backend', 'app_id']),
|
||||
clearHash: this.props.clearHash,
|
||||
@ -36,13 +36,16 @@ export default class GitLabAuthenticationPage extends React.Component {
|
||||
} else {
|
||||
this.auth = new NetlifyAuthenticator({
|
||||
base_url: this.props.base_url,
|
||||
site_id: (document.location.host.split(':')[0] === 'localhost') ? 'cms.netlify.com' : this.props.siteId,
|
||||
site_id:
|
||||
document.location.host.split(':')[0] === 'localhost'
|
||||
? 'cms.netlify.com'
|
||||
: this.props.siteId,
|
||||
auth_endpoint: this.props.authEndpoint,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
handleLogin = (e) => {
|
||||
handleLogin = e => {
|
||||
e.preventDefault();
|
||||
this.auth.authenticate({ provider: 'gitlab', scope: 'api' }, (err, data) => {
|
||||
if (err) {
|
||||
@ -62,7 +65,7 @@ export default class GitLabAuthenticationPage extends React.Component {
|
||||
loginErrorMessage={this.state.loginError}
|
||||
renderButtonContent={() => (
|
||||
<React.Fragment>
|
||||
<LoginButtonIcon type="gitlab"/> {inProgress ? "Logging in..." : "Login with GitLab"}
|
||||
<LoginButtonIcon type="gitlab" /> {inProgress ? 'Logging in...' : 'Login with GitLab'}
|
||||
</React.Fragment>
|
||||
)}
|
||||
/>
|
||||
|
@ -1,8 +1,8 @@
|
||||
import trimStart from 'lodash/trimStart';
|
||||
import semaphore from "semaphore";
|
||||
import semaphore from 'semaphore';
|
||||
import { CURSOR_COMPATIBILITY_SYMBOL } from 'netlify-cms-lib-util';
|
||||
import AuthenticationPage from "./AuthenticationPage";
|
||||
import API from "./API";
|
||||
import AuthenticationPage from './AuthenticationPage';
|
||||
import API from './API';
|
||||
|
||||
const MAX_CONCURRENT_DOWNLOADS = 10;
|
||||
|
||||
@ -16,18 +16,18 @@ export default class GitLab {
|
||||
};
|
||||
|
||||
if (this.options.useWorkflow) {
|
||||
throw new Error("The GitLab backend does not support the Editorial Workflow.")
|
||||
throw new Error('The GitLab backend does not support the Editorial Workflow.');
|
||||
}
|
||||
|
||||
if (!this.options.proxied && config.getIn(["backend", "repo"]) == null) {
|
||||
throw new Error("The GitLab backend needs a \"repo\" in the backend configuration.");
|
||||
if (!this.options.proxied && config.getIn(['backend', 'repo']) == null) {
|
||||
throw new Error('The GitLab backend needs a "repo" in the backend configuration.');
|
||||
}
|
||||
|
||||
this.api = this.options.API || null;
|
||||
|
||||
this.repo = config.getIn(["backend", "repo"], "");
|
||||
this.branch = config.getIn(["backend", "branch"], "master");
|
||||
this.api_root = config.getIn(["backend", "api_root"], "https://gitlab.com/api/v4");
|
||||
this.repo = config.getIn(['backend', 'repo'], '');
|
||||
this.branch = config.getIn(['backend', 'branch'], 'master');
|
||||
this.api_root = config.getIn(['backend', 'api_root'], 'https://gitlab.com/api/v4');
|
||||
this.token = '';
|
||||
}
|
||||
|
||||
@ -41,14 +41,20 @@ export default class GitLab {
|
||||
|
||||
authenticate(state) {
|
||||
this.token = state.token;
|
||||
this.api = new API({ token: this.token, branch: this.branch, repo: this.repo, api_root: this.api_root });
|
||||
this.api = new API({
|
||||
token: this.token,
|
||||
branch: this.branch,
|
||||
repo: this.repo,
|
||||
api_root: this.api_root,
|
||||
});
|
||||
return this.api.user().then(user =>
|
||||
this.api.hasWriteAccess(user).then((isCollab) => {
|
||||
this.api.hasWriteAccess(user).then(isCollab => {
|
||||
// Unauthorized user
|
||||
if (!isCollab) throw new Error("Your GitLab user account does not have access to this repo.");
|
||||
if (!isCollab)
|
||||
throw new Error('Your GitLab user account does not have access to this repo.');
|
||||
// Authorized user
|
||||
return Object.assign({}, user, { token: state.token });
|
||||
})
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
@ -62,32 +68,27 @@ export default class GitLab {
|
||||
}
|
||||
|
||||
entriesByFolder(collection, extension) {
|
||||
return this.api.listFiles(collection.get("folder"))
|
||||
.then(({ files, cursor }) =>
|
||||
this.fetchFiles(
|
||||
files.filter(file => file.name.endsWith('.' + extension))
|
||||
)
|
||||
.then(fetchedFiles => {
|
||||
const returnedFiles = fetchedFiles;
|
||||
returnedFiles[CURSOR_COMPATIBILITY_SYMBOL] = cursor;
|
||||
return returnedFiles;
|
||||
})
|
||||
return this.api.listFiles(collection.get('folder')).then(({ files, cursor }) =>
|
||||
this.fetchFiles(files.filter(file => file.name.endsWith('.' + extension))).then(
|
||||
fetchedFiles => {
|
||||
const returnedFiles = fetchedFiles;
|
||||
returnedFiles[CURSOR_COMPATIBILITY_SYMBOL] = cursor;
|
||||
return returnedFiles;
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
allEntriesByFolder(collection, extension) {
|
||||
return this.api.listAllFiles(collection.get("folder"))
|
||||
.then(files =>
|
||||
this.fetchFiles(
|
||||
files.filter(file => file.name.endsWith('.' + extension))
|
||||
)
|
||||
);
|
||||
return this.api
|
||||
.listAllFiles(collection.get('folder'))
|
||||
.then(files => this.fetchFiles(files.filter(file => file.name.endsWith('.' + extension))));
|
||||
}
|
||||
|
||||
entriesByFiles(collection) {
|
||||
const files = collection.get("files").map(collectionFile => ({
|
||||
path: collectionFile.get("file"),
|
||||
label: collectionFile.get("label"),
|
||||
const files = collection.get('files').map(collectionFile => ({
|
||||
path: collectionFile.get('file'),
|
||||
label: collectionFile.get('label'),
|
||||
}));
|
||||
return this.fetchFiles(files).then(fetchedFiles => {
|
||||
const returnedFiles = fetchedFiles;
|
||||
@ -95,23 +96,31 @@ export default class GitLab {
|
||||
});
|
||||
}
|
||||
|
||||
fetchFiles = (files) => {
|
||||
fetchFiles = files => {
|
||||
const sem = semaphore(MAX_CONCURRENT_DOWNLOADS);
|
||||
const promises = [];
|
||||
files.forEach((file) => {
|
||||
promises.push(new Promise(resolve => (
|
||||
sem.take(() => this.api.readFile(file.path, file.id).then((data) => {
|
||||
resolve({ file, data });
|
||||
sem.leave();
|
||||
}).catch((error = true) => {
|
||||
sem.leave();
|
||||
console.error(`failed to load file from GitLab: ${ file.path }`);
|
||||
resolve({ error });
|
||||
}))
|
||||
)));
|
||||
files.forEach(file => {
|
||||
promises.push(
|
||||
new Promise(resolve =>
|
||||
sem.take(() =>
|
||||
this.api
|
||||
.readFile(file.path, file.id)
|
||||
.then(data => {
|
||||
resolve({ file, data });
|
||||
sem.leave();
|
||||
})
|
||||
.catch((error = true) => {
|
||||
sem.leave();
|
||||
console.error(`failed to load file from GitLab: ${file.path}`);
|
||||
resolve({ error });
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
return Promise.all(promises)
|
||||
.then(loadedEntries => loadedEntries.filter(loadedEntry => !loadedEntry.error));
|
||||
return Promise.all(promises).then(loadedEntries =>
|
||||
loadedEntries.filter(loadedEntry => !loadedEntry.error),
|
||||
);
|
||||
};
|
||||
|
||||
// Fetches a single entry.
|
||||
@ -123,17 +132,17 @@ export default class GitLab {
|
||||
}
|
||||
|
||||
getMedia() {
|
||||
return this.api.listAllFiles(this.config.get('media_folder'))
|
||||
.then(files => files.map(({ id, name, path }) => {
|
||||
return this.api.listAllFiles(this.config.get('media_folder')).then(files =>
|
||||
files.map(({ id, name, path }) => {
|
||||
const url = new URL(this.api.fileDownloadURL(path));
|
||||
if (url.pathname.match(/.svg$/)) {
|
||||
url.search += (url.search.slice(1) === '' ? '?' : '&') + 'sanitize=true';
|
||||
}
|
||||
return { id, name, url: url.href, path };
|
||||
}));
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
async persistEntry(entry, mediaFiles, options = {}) {
|
||||
return this.api.persistFiles([entry], options);
|
||||
}
|
||||
@ -150,10 +159,11 @@ export default class GitLab {
|
||||
}
|
||||
|
||||
traverseCursor(cursor, action) {
|
||||
return this.api.traverseCursor(cursor, action)
|
||||
.then(async ({ entries, cursor: newCursor }) => ({
|
||||
entries: await Promise.all(entries.map(file => this.api.readFile(file.path, file.id).then(data => ({ file, data })))),
|
||||
cursor: newCursor,
|
||||
}));
|
||||
return this.api.traverseCursor(cursor, action).then(async ({ entries, cursor: newCursor }) => ({
|
||||
entries: await Promise.all(
|
||||
entries.map(file => this.api.readFile(file.path, file.id).then(data => ({ file, data }))),
|
||||
),
|
||||
cursor: newCursor,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,3 @@
|
||||
export GitLabBackend from './implementation';
|
||||
export API from './API';
|
||||
export AuthenticationPage from './AuthenticationPage';
|
||||
|
||||
|
Reference in New Issue
Block a user