2018-08-07 14:46:54 -06:00
|
|
|
import GoTrue from 'gotrue-js';
|
2017-01-26 19:23:42 -02:00
|
|
|
import jwtDecode from 'jwt-decode';
|
2019-02-26 10:11:15 -08:00
|
|
|
import { fromPairs, get, pick, intersection, unzip } from 'lodash';
|
|
|
|
import ini from 'ini';
|
2020-01-15 00:15:14 +02:00
|
|
|
import {
|
|
|
|
APIError,
|
|
|
|
unsentRequest,
|
|
|
|
basename,
|
|
|
|
ApiRequest,
|
|
|
|
AssetProxy,
|
|
|
|
PersistOptions,
|
|
|
|
Entry,
|
|
|
|
Cursor,
|
|
|
|
Implementation,
|
|
|
|
DisplayURL,
|
|
|
|
User,
|
|
|
|
Credentials,
|
|
|
|
entriesByFiles,
|
|
|
|
Config,
|
|
|
|
ImplementationFile,
|
|
|
|
UnpublishedEntryMediaFile,
|
2020-01-21 18:57:36 +02:00
|
|
|
parsePointerFile,
|
|
|
|
getLargeMediaPatternsFromGitAttributesFile,
|
|
|
|
PointerFile,
|
|
|
|
getPointerFileForMediaFileObj,
|
|
|
|
getLargeMediaFilteredMediaFiles,
|
2020-01-15 00:15:14 +02:00
|
|
|
} from 'netlify-cms-lib-util';
|
2018-08-07 14:46:54 -06:00
|
|
|
import { GitHubBackend } from 'netlify-cms-backend-github';
|
|
|
|
import { GitLabBackend } from 'netlify-cms-backend-gitlab';
|
2019-03-16 15:44:29 -07:00
|
|
|
import { BitbucketBackend, API as BitBucketAPI } from 'netlify-cms-backend-bitbucket';
|
2018-08-07 14:46:54 -06:00
|
|
|
import GitHubAPI from './GitHubAPI';
|
|
|
|
import GitLabAPI from './GitLabAPI';
|
|
|
|
import AuthenticationPage from './AuthenticationPage';
|
2020-01-21 18:57:36 +02:00
|
|
|
import { getClient, Client } from './netlify-lfs-client';
|
2016-12-23 16:59:48 -02:00
|
|
|
|
2020-01-15 00:15:14 +02:00
|
|
|
declare global {
|
|
|
|
interface Window {
|
|
|
|
netlifyIdentity?: { gotrue: GoTrue; logout: () => void };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const localHosts: Record<string, boolean> = {
|
2017-09-05 13:24:16 -07:00
|
|
|
localhost: true,
|
|
|
|
'127.0.0.1': true,
|
2018-06-11 19:03:43 -07:00
|
|
|
'0.0.0.0': true,
|
|
|
|
};
|
2017-09-05 19:30:03 -07:00
|
|
|
const defaults = {
|
|
|
|
identity: '/.netlify/identity',
|
2018-06-11 19:03:43 -07:00
|
|
|
gateway: '/.netlify/git',
|
2019-02-26 10:11:15 -08:00
|
|
|
largeMedia: '/.netlify/large-media',
|
2018-06-11 19:03:43 -07:00
|
|
|
};
|
2017-09-05 13:24:16 -07:00
|
|
|
|
2020-01-15 00:15:14 +02:00
|
|
|
function getEndpoint(endpoint: string, netlifySiteURL: string | null) {
|
2018-08-07 14:46:54 -06:00
|
|
|
if (
|
2020-01-15 00:15:14 +02:00
|
|
|
localHosts[document.location.host.split(':').shift() as string] &&
|
2018-08-07 14:46:54 -06:00
|
|
|
netlifySiteURL &&
|
|
|
|
endpoint.match(/^\/\.netlify\//)
|
|
|
|
) {
|
2017-09-05 19:30:03 -07:00
|
|
|
const parts = [];
|
|
|
|
if (netlifySiteURL) {
|
|
|
|
parts.push(netlifySiteURL);
|
2018-08-07 14:46:54 -06:00
|
|
|
if (!netlifySiteURL.match(/\/$/)) {
|
|
|
|
parts.push('/');
|
|
|
|
}
|
2017-09-05 19:30:03 -07:00
|
|
|
}
|
|
|
|
parts.push(endpoint.replace(/^\//, ''));
|
2018-08-07 14:46:54 -06:00
|
|
|
return parts.join('');
|
2017-09-05 13:24:16 -07:00
|
|
|
}
|
|
|
|
return endpoint;
|
|
|
|
}
|
|
|
|
|
2020-01-15 00:15:14 +02:00
|
|
|
interface NetlifyUser extends Credentials {
|
|
|
|
jwt: () => Promise<string>;
|
|
|
|
email: string;
|
|
|
|
user_metadata: { full_name: string; avatar_url: string };
|
|
|
|
}
|
|
|
|
|
|
|
|
interface GetMediaDisplayURLArgs {
|
|
|
|
path: string;
|
|
|
|
original: { id: string; path: string } | string;
|
|
|
|
largeMedia: PointerFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class GitGateway implements Implementation {
|
|
|
|
config: Config;
|
|
|
|
api?: GitHubAPI | GitLabAPI | BitBucketAPI;
|
|
|
|
branch: string;
|
|
|
|
squashMerges: boolean;
|
|
|
|
mediaFolder: string;
|
|
|
|
transformImages: boolean;
|
|
|
|
gatewayUrl: string;
|
|
|
|
netlifyLargeMediaURL: string;
|
|
|
|
backendType: string | null;
|
|
|
|
authClient: GoTrue;
|
|
|
|
backend: GitHubBackend | GitLabBackend | BitbucketBackend | null;
|
|
|
|
acceptRoles?: string[];
|
|
|
|
tokenPromise?: () => Promise<string>;
|
|
|
|
_largeMediaClientPromise?: Promise<Client>;
|
|
|
|
|
|
|
|
options: {
|
|
|
|
proxied: boolean;
|
|
|
|
API: GitHubAPI | GitLabAPI | BitBucketAPI | null;
|
|
|
|
initialWorkflowStatus: string;
|
|
|
|
};
|
|
|
|
constructor(config: Config, options = {}) {
|
2018-07-27 18:40:53 -04:00
|
|
|
this.options = {
|
2018-07-28 13:41:45 -07:00
|
|
|
proxied: true,
|
2018-07-27 18:40:53 -04:00
|
|
|
API: null,
|
2020-01-15 00:15:14 +02:00
|
|
|
initialWorkflowStatus: '',
|
2018-07-27 18:40:53 -04:00
|
|
|
...options,
|
|
|
|
};
|
2018-06-11 19:03:43 -07:00
|
|
|
this.config = config;
|
2020-01-15 00:15:14 +02:00
|
|
|
this.branch = config.backend.branch?.trim() || 'master';
|
|
|
|
this.squashMerges = config.backend.squash_merges || false;
|
|
|
|
this.mediaFolder = config.media_folder;
|
|
|
|
this.transformImages = config.backend.use_large_media_transforms_in_media_library || true;
|
2017-01-26 19:23:42 -02:00
|
|
|
|
2018-08-07 14:46:54 -06:00
|
|
|
const netlifySiteURL = localStorage.getItem('netlifySiteURL');
|
2020-01-15 00:15:14 +02:00
|
|
|
const APIUrl = getEndpoint(config.backend.identity_url || defaults.identity, netlifySiteURL);
|
|
|
|
this.gatewayUrl = getEndpoint(config.backend.gateway_url || defaults.gateway, netlifySiteURL);
|
2019-02-26 10:11:15 -08:00
|
|
|
this.netlifyLargeMediaURL = getEndpoint(
|
2020-01-15 00:15:14 +02:00
|
|
|
config.backend.large_media_url || defaults.largeMedia,
|
2019-02-26 10:11:15 -08:00
|
|
|
netlifySiteURL,
|
|
|
|
);
|
2018-07-19 17:32:40 -07:00
|
|
|
const backendTypeRegex = /\/(github|gitlab|bitbucket)\/?$/;
|
2018-06-11 19:03:43 -07:00
|
|
|
const backendTypeMatches = this.gatewayUrl.match(backendTypeRegex);
|
|
|
|
if (backendTypeMatches) {
|
|
|
|
this.backendType = backendTypeMatches[1];
|
2018-11-21 18:49:39 -07:00
|
|
|
this.gatewayUrl = this.gatewayUrl.replace(backendTypeRegex, '');
|
2018-06-11 19:03:43 -07:00
|
|
|
} else {
|
|
|
|
this.backendType = null;
|
|
|
|
}
|
2016-12-23 16:59:48 -02:00
|
|
|
|
2018-08-07 14:46:54 -06:00
|
|
|
this.authClient = window.netlifyIdentity
|
|
|
|
? window.netlifyIdentity.gotrue
|
|
|
|
: new GoTrue({ APIUrl });
|
2016-12-23 16:59:48 -02:00
|
|
|
AuthenticationPage.authClient = this.authClient;
|
|
|
|
|
2018-06-11 19:03:43 -07:00
|
|
|
this.backend = null;
|
2016-12-23 16:59:48 -02:00
|
|
|
}
|
2018-07-19 17:32:40 -07:00
|
|
|
|
2020-01-15 00:15:14 +02:00
|
|
|
requestFunction = (req: ApiRequest) =>
|
|
|
|
this.tokenPromise!()
|
|
|
|
.then(
|
|
|
|
token => unsentRequest.withHeaders({ Authorization: `Bearer ${token}` }, req) as ApiRequest,
|
|
|
|
)
|
2018-08-07 14:46:54 -06:00
|
|
|
.then(unsentRequest.performRequest);
|
2018-07-19 17:32:40 -07:00
|
|
|
|
2020-01-15 00:15:14 +02:00
|
|
|
authenticate(credentials: Credentials) {
|
|
|
|
const user = credentials as NetlifyUser;
|
2017-01-10 22:23:22 -02:00
|
|
|
this.tokenPromise = user.jwt.bind(user);
|
2020-01-15 00:15:14 +02:00
|
|
|
return this.tokenPromise!().then(async token => {
|
2018-06-11 19:03:43 -07:00
|
|
|
if (!this.backendType) {
|
2020-01-15 00:15:14 +02:00
|
|
|
const {
|
|
|
|
github_enabled: githubEnabled,
|
|
|
|
gitlab_enabled: gitlabEnabled,
|
|
|
|
bitbucket_enabled: bitbucketEnabled,
|
|
|
|
roles,
|
|
|
|
} = await fetch(`${this.gatewayUrl}/settings`, {
|
|
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
|
|
}).then(async res => {
|
|
|
|
const contentType = res.headers.get('Content-Type') || '';
|
2018-12-11 11:24:32 -05:00
|
|
|
if (!contentType.includes('application/json') && !contentType.includes('text/json')) {
|
2018-09-04 09:08:13 -06:00
|
|
|
throw new APIError(
|
|
|
|
`Your Git Gateway backend is not returning valid settings. Please make sure it is enabled.`,
|
|
|
|
res.status,
|
|
|
|
'Git Gateway',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const body = await res.json();
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
throw new APIError(
|
|
|
|
`Git Gateway Error: ${body.message ? body.message : body}`,
|
|
|
|
res.status,
|
|
|
|
'Git Gateway',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return body;
|
|
|
|
});
|
2018-06-11 19:03:43 -07:00
|
|
|
this.acceptRoles = roles;
|
2020-01-15 00:15:14 +02:00
|
|
|
if (githubEnabled) {
|
2018-08-07 14:46:54 -06:00
|
|
|
this.backendType = 'github';
|
2020-01-15 00:15:14 +02:00
|
|
|
} else if (gitlabEnabled) {
|
2018-08-07 14:46:54 -06:00
|
|
|
this.backendType = 'gitlab';
|
2020-01-15 00:15:14 +02:00
|
|
|
} else if (bitbucketEnabled) {
|
2018-08-07 14:46:54 -06:00
|
|
|
this.backendType = 'bitbucket';
|
2018-06-11 19:03:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.acceptRoles && this.acceptRoles.length > 0) {
|
2017-11-10 16:33:22 -07:00
|
|
|
const userRoles = get(jwtDecode(token), 'app_metadata.roles', []);
|
2018-06-11 19:03:43 -07:00
|
|
|
const validRole = intersection(userRoles, this.acceptRoles).length > 0;
|
|
|
|
if (!validRole) {
|
|
|
|
throw new Error("You don't have sufficient permissions to access Netlify CMS");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const userData = {
|
2020-01-15 00:15:14 +02:00
|
|
|
name: user.user_metadata.full_name || user.email.split('@').shift()!,
|
2018-06-11 19:03:43 -07:00
|
|
|
email: user.email,
|
2020-01-15 00:15:14 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/camelcase
|
2018-06-11 19:03:43 -07:00
|
|
|
avatar_url: user.user_metadata.avatar_url,
|
|
|
|
metadata: user.user_metadata,
|
|
|
|
};
|
|
|
|
const apiConfig = {
|
2020-01-15 00:15:14 +02:00
|
|
|
apiRoot: `${this.gatewayUrl}/${this.backendType}`,
|
2018-06-11 19:03:43 -07:00
|
|
|
branch: this.branch,
|
2020-01-15 00:15:14 +02:00
|
|
|
tokenPromise: this.tokenPromise!,
|
2018-08-07 14:46:54 -06:00
|
|
|
commitAuthor: pick(userData, ['name', 'email']),
|
2020-01-15 00:15:14 +02:00
|
|
|
squashMerges: this.squashMerges,
|
2018-08-06 13:58:56 -06:00
|
|
|
initialWorkflowStatus: this.options.initialWorkflowStatus,
|
2018-06-11 19:03:43 -07:00
|
|
|
};
|
|
|
|
|
2018-08-07 14:46:54 -06:00
|
|
|
if (this.backendType === 'github') {
|
2018-06-11 19:03:43 -07:00
|
|
|
this.api = new GitHubAPI(apiConfig);
|
2018-07-27 18:40:53 -04:00
|
|
|
this.backend = new GitHubBackend(this.config, { ...this.options, API: this.api });
|
2018-08-07 14:46:54 -06:00
|
|
|
} else if (this.backendType === 'gitlab') {
|
2018-06-11 19:03:43 -07:00
|
|
|
this.api = new GitLabAPI(apiConfig);
|
2018-07-27 18:40:53 -04:00
|
|
|
this.backend = new GitLabBackend(this.config, { ...this.options, API: this.api });
|
2018-08-07 14:46:54 -06:00
|
|
|
} else if (this.backendType === 'bitbucket') {
|
2018-07-19 17:32:40 -07:00
|
|
|
this.api = new BitBucketAPI({
|
|
|
|
...apiConfig,
|
|
|
|
requestFunction: this.requestFunction,
|
|
|
|
hasWriteAccess: async () => true,
|
|
|
|
});
|
2019-03-16 15:44:29 -07:00
|
|
|
this.backend = new BitbucketBackend(this.config, { ...this.options, API: this.api });
|
2017-09-05 16:48:51 -07:00
|
|
|
}
|
2018-06-11 19:03:43 -07:00
|
|
|
|
2020-01-15 00:15:14 +02:00
|
|
|
if (!(await this.api!.hasWriteAccess())) {
|
2017-09-05 16:48:51 -07:00
|
|
|
throw new Error("You don't have sufficient permissions to access Netlify CMS");
|
2017-01-26 19:23:42 -02:00
|
|
|
}
|
2020-01-15 00:15:14 +02:00
|
|
|
return { name: userData.name, login: userData.email } as User;
|
2018-06-11 19:03:43 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
restoreUser() {
|
|
|
|
const user = this.authClient && this.authClient.currentUser();
|
|
|
|
if (!user) return Promise.reject();
|
2020-01-15 00:15:14 +02:00
|
|
|
return this.authenticate(user as Credentials);
|
2018-06-11 19:03:43 -07:00
|
|
|
}
|
|
|
|
authComponent() {
|
|
|
|
return AuthenticationPage;
|
2017-01-10 22:23:22 -02:00
|
|
|
}
|
2020-01-15 00:15:14 +02:00
|
|
|
|
2017-09-05 19:30:03 -07:00
|
|
|
logout() {
|
|
|
|
if (window.netlifyIdentity) {
|
|
|
|
return window.netlifyIdentity.logout();
|
|
|
|
}
|
|
|
|
const user = this.authClient.currentUser();
|
|
|
|
return user && user.logout();
|
|
|
|
}
|
2017-01-10 22:23:22 -02:00
|
|
|
getToken() {
|
2020-01-15 00:15:14 +02:00
|
|
|
return this.tokenPromise!();
|
2016-12-23 16:59:48 -02:00
|
|
|
}
|
|
|
|
|
2020-01-15 00:15:14 +02:00
|
|
|
entriesByFolder(folder: string, extension: string, depth: number) {
|
|
|
|
return this.backend!.entriesByFolder(folder, extension, depth);
|
2018-08-07 14:46:54 -06:00
|
|
|
}
|
2020-01-15 00:15:14 +02:00
|
|
|
entriesByFiles(files: ImplementationFile[]) {
|
|
|
|
return this.backend!.entriesByFiles(files);
|
2018-08-07 14:46:54 -06:00
|
|
|
}
|
2020-01-15 00:15:14 +02:00
|
|
|
getEntry(path: string) {
|
|
|
|
return this.backend!.getEntry(path);
|
2018-08-07 14:46:54 -06:00
|
|
|
}
|
2019-02-26 10:11:15 -08:00
|
|
|
|
2020-01-15 00:15:14 +02:00
|
|
|
async loadEntryMediaFiles(branch: string, files: UnpublishedEntryMediaFile[]) {
|
2019-12-18 18:16:02 +02:00
|
|
|
const client = await this.getLargeMediaClient();
|
2020-01-15 00:15:14 +02:00
|
|
|
const backend = this.backend as GitLabBackend | GitHubBackend;
|
2019-12-18 18:16:02 +02:00
|
|
|
if (!client.enabled) {
|
2020-01-15 00:15:14 +02:00
|
|
|
return backend!.loadEntryMediaFiles(branch, files);
|
2019-12-18 18:16:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const mediaFiles = await Promise.all(
|
|
|
|
files.map(async file => {
|
|
|
|
if (client.matchPath(file.path)) {
|
2020-01-15 00:15:14 +02:00
|
|
|
const { id, path } = file;
|
2020-01-19 14:25:29 +02:00
|
|
|
const largeMediaDisplayURLs = await this.getLargeMediaDisplayURLs(
|
|
|
|
[{ ...file, id }],
|
|
|
|
branch,
|
|
|
|
);
|
2019-12-18 18:16:02 +02:00
|
|
|
const url = await client.getDownloadURL(largeMediaDisplayURLs[id]);
|
|
|
|
return {
|
|
|
|
id,
|
|
|
|
name: basename(path),
|
|
|
|
path,
|
|
|
|
url,
|
|
|
|
displayURL: url,
|
2020-01-15 00:15:14 +02:00
|
|
|
file: new File([], name),
|
|
|
|
size: 0,
|
2019-12-18 18:16:02 +02:00
|
|
|
};
|
|
|
|
} else {
|
2020-01-15 00:15:14 +02:00
|
|
|
return backend!.loadMediaFile(branch, file);
|
2019-12-18 18:16:02 +02:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
return mediaFiles;
|
|
|
|
}
|
|
|
|
|
2020-01-15 00:15:14 +02:00
|
|
|
getMedia(mediaFolder = this.mediaFolder) {
|
|
|
|
return Promise.all([this.backend!.getMedia(mediaFolder), this.getLargeMediaClient()]).then(
|
2019-02-26 10:11:15 -08:00
|
|
|
async ([mediaFiles, largeMediaClient]) => {
|
|
|
|
if (!largeMediaClient.enabled) {
|
2019-04-05 15:25:54 -07:00
|
|
|
return mediaFiles.map(({ displayURL, ...rest }) => ({
|
|
|
|
...rest,
|
|
|
|
displayURL: { original: displayURL },
|
|
|
|
}));
|
2019-02-26 10:11:15 -08:00
|
|
|
}
|
2019-12-18 18:16:02 +02:00
|
|
|
if (mediaFiles.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
2019-03-07 21:28:14 -05:00
|
|
|
const largeMediaDisplayURLs = await this.getLargeMediaDisplayURLs(mediaFiles);
|
|
|
|
return mediaFiles.map(({ id, displayURL, path, ...rest }) => {
|
|
|
|
return {
|
|
|
|
...rest,
|
|
|
|
id,
|
|
|
|
path,
|
|
|
|
displayURL: {
|
|
|
|
path,
|
|
|
|
original: displayURL,
|
|
|
|
largeMedia: largeMediaDisplayURLs[id],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
2019-02-26 10:11:15 -08:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// this method memoizes this._getLargeMediaClient so that there can
|
|
|
|
// only be one client at a time
|
|
|
|
getLargeMediaClient() {
|
|
|
|
if (this._largeMediaClientPromise) {
|
|
|
|
return this._largeMediaClientPromise;
|
|
|
|
}
|
|
|
|
this._largeMediaClientPromise = this._getLargeMediaClient();
|
|
|
|
return this._largeMediaClientPromise;
|
|
|
|
}
|
|
|
|
_getLargeMediaClient() {
|
2020-01-15 00:15:14 +02:00
|
|
|
const netlifyLargeMediaEnabledPromise = this.api!.readFile('.lfsconfig')
|
|
|
|
.then(config => ini.decode<{ lfs: { url: string } }>(config as string))
|
2019-02-26 10:11:15 -08:00
|
|
|
.then(({ lfs: { url } }) => new URL(url))
|
|
|
|
.then(lfsURL => ({ enabled: lfsURL.hostname.endsWith('netlify.com') }))
|
2020-01-15 00:15:14 +02:00
|
|
|
.catch((err: Error) => ({ enabled: false, err }));
|
2019-02-26 10:11:15 -08:00
|
|
|
|
2020-01-15 00:15:14 +02:00
|
|
|
const lfsPatternsPromise = this.api!.readFile('.gitattributes')
|
|
|
|
.then(attributes => getLargeMediaPatternsFromGitAttributesFile(attributes as string))
|
|
|
|
.then((patterns: string[]) => ({ err: null, patterns }))
|
|
|
|
.catch((err: Error) => {
|
2019-11-10 00:34:03 -08:00
|
|
|
if (err.message.includes('404')) {
|
|
|
|
console.log('This 404 was expected and handled appropriately.');
|
2020-01-15 00:15:14 +02:00
|
|
|
return { err: null, patterns: [] as string[] };
|
2019-11-10 00:34:03 -08:00
|
|
|
} else {
|
2020-01-15 00:15:14 +02:00
|
|
|
return { err, patterns: [] as string[] };
|
2019-11-10 00:34:03 -08:00
|
|
|
}
|
|
|
|
});
|
2019-02-26 10:11:15 -08:00
|
|
|
|
|
|
|
return Promise.all([netlifyLargeMediaEnabledPromise, lfsPatternsPromise]).then(
|
|
|
|
([{ enabled: maybeEnabled }, { patterns, err: patternsErr }]) => {
|
|
|
|
const enabled = maybeEnabled && !patternsErr;
|
|
|
|
|
|
|
|
// We expect LFS patterns to exist when the .lfsconfig states
|
|
|
|
// that we're using Netlify Large Media
|
|
|
|
if (maybeEnabled && patternsErr) {
|
|
|
|
console.error(patternsErr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return getClient({
|
|
|
|
enabled,
|
|
|
|
rootURL: this.netlifyLargeMediaURL,
|
|
|
|
makeAuthorizedRequest: this.requestFunction,
|
|
|
|
patterns,
|
2020-01-15 00:15:14 +02:00
|
|
|
transformImages: this.transformImages
|
|
|
|
? // eslint-disable-next-line @typescript-eslint/camelcase
|
|
|
|
{ nf_resize: 'fit', w: 560, h: 320 }
|
2019-02-26 10:11:15 -08:00
|
|
|
: false,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2020-01-19 14:25:29 +02:00
|
|
|
async getLargeMediaDisplayURLs(
|
|
|
|
mediaFiles: { path: string; id: string | null }[],
|
|
|
|
branch = this.branch,
|
|
|
|
) {
|
2019-03-22 06:59:06 -07:00
|
|
|
const client = await this.getLargeMediaClient();
|
2020-01-19 14:25:29 +02:00
|
|
|
const readFile = (
|
|
|
|
path: string,
|
|
|
|
id: string | null | undefined,
|
|
|
|
{ parseText }: { parseText: boolean },
|
|
|
|
) => this.api!.readFile(path, id, { branch, parseText });
|
|
|
|
|
|
|
|
const filesPromise = entriesByFiles(mediaFiles, readFile, 'Git-Gateway');
|
2020-01-15 00:15:14 +02:00
|
|
|
|
|
|
|
return filesPromise
|
2019-03-22 06:59:06 -07:00
|
|
|
.then(items =>
|
2020-01-15 18:23:42 +02:00
|
|
|
items.map(({ file: { id, path }, data }) => {
|
2019-03-22 06:59:06 -07:00
|
|
|
const parsedPointerFile = parsePointerFile(data);
|
2020-01-15 18:23:42 +02:00
|
|
|
if (!parsedPointerFile.sha) {
|
|
|
|
console.warn(`Failed parsing pointer file ${path}`);
|
|
|
|
}
|
2019-03-22 06:59:06 -07:00
|
|
|
return [
|
|
|
|
{
|
2020-01-15 00:15:14 +02:00
|
|
|
pointerId: id,
|
2019-03-22 06:59:06 -07:00
|
|
|
resourceId: parsedPointerFile.sha,
|
|
|
|
},
|
|
|
|
parsedPointerFile,
|
|
|
|
];
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.then(unzip)
|
|
|
|
.then(([idMaps, files]) =>
|
2020-01-15 00:15:14 +02:00
|
|
|
Promise.all([
|
|
|
|
idMaps as { pointerId: string; resourceId: string }[],
|
|
|
|
client.getResourceDownloadURLArgs(files as PointerFile[]).then(r => fromPairs(r)),
|
|
|
|
]),
|
2019-03-22 06:59:06 -07:00
|
|
|
)
|
|
|
|
.then(([idMaps, resourceMap]) =>
|
|
|
|
idMaps.map(({ pointerId, resourceId }) => [pointerId, resourceMap[resourceId]]),
|
|
|
|
)
|
|
|
|
.then(fromPairs);
|
2018-08-07 14:46:54 -06:00
|
|
|
}
|
2019-03-07 21:28:14 -05:00
|
|
|
|
2020-01-15 00:15:14 +02:00
|
|
|
getMediaDisplayURL(displayURL: DisplayURL) {
|
|
|
|
const {
|
|
|
|
path,
|
|
|
|
original,
|
|
|
|
largeMedia: largeMediaDisplayURL,
|
|
|
|
} = (displayURL as unknown) as GetMediaDisplayURLArgs;
|
2019-03-07 21:28:14 -05:00
|
|
|
return this.getLargeMediaClient().then(client => {
|
|
|
|
if (client.enabled && client.matchPath(path)) {
|
|
|
|
return client.getDownloadURL(largeMediaDisplayURL);
|
|
|
|
}
|
2019-03-08 12:26:27 -08:00
|
|
|
if (typeof original === 'string') {
|
|
|
|
return original;
|
|
|
|
}
|
2020-01-15 00:15:14 +02:00
|
|
|
if (this.backend!.getMediaDisplayURL) {
|
|
|
|
return this.backend!.getMediaDisplayURL(original);
|
2019-03-07 21:28:14 -05:00
|
|
|
}
|
|
|
|
const err = new Error(
|
2019-06-26 14:28:00 -04:00
|
|
|
`getMediaDisplayURL is not implemented by the ${this.backendType} backend, but the backend returned a displayURL which was not a string!`,
|
2020-01-15 00:15:14 +02:00
|
|
|
) as Error & {
|
|
|
|
displayURL: DisplayURL;
|
|
|
|
};
|
2019-03-07 21:28:14 -05:00
|
|
|
err.displayURL = displayURL;
|
|
|
|
return Promise.reject(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-15 00:15:14 +02:00
|
|
|
async getMediaFile(path: string) {
|
2019-12-18 18:16:02 +02:00
|
|
|
const client = await this.getLargeMediaClient();
|
|
|
|
if (client.enabled && client.matchPath(path)) {
|
2020-01-15 00:15:14 +02:00
|
|
|
const largeMediaDisplayURLs = await this.getLargeMediaDisplayURLs([{ path, id: null }]);
|
2019-12-18 18:16:02 +02:00
|
|
|
const url = await client.getDownloadURL(Object.values(largeMediaDisplayURLs)[0]);
|
|
|
|
return {
|
2020-01-15 00:15:14 +02:00
|
|
|
id: url,
|
2019-12-18 18:16:02 +02:00
|
|
|
name: basename(path),
|
|
|
|
path,
|
|
|
|
url,
|
|
|
|
displayURL: url,
|
|
|
|
};
|
|
|
|
}
|
2020-01-15 00:15:14 +02:00
|
|
|
return this.backend!.getMediaFile(path);
|
2019-12-18 18:16:02 +02:00
|
|
|
}
|
|
|
|
|
2020-01-15 00:15:14 +02:00
|
|
|
async persistEntry(entry: Entry, mediaFiles: AssetProxy[], options: PersistOptions) {
|
2019-03-22 06:59:06 -07:00
|
|
|
const client = await this.getLargeMediaClient();
|
2020-01-21 18:57:36 +02:00
|
|
|
return this.backend!.persistEntry(
|
|
|
|
entry,
|
|
|
|
client.enabled ? await getLargeMediaFilteredMediaFiles(client, mediaFiles) : mediaFiles,
|
|
|
|
options,
|
2019-03-22 06:59:06 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-15 00:15:14 +02:00
|
|
|
async persistMedia(mediaFile: AssetProxy, options: PersistOptions) {
|
|
|
|
const { fileObj, path } = mediaFile;
|
2019-03-22 06:59:06 -07:00
|
|
|
const displayURL = URL.createObjectURL(fileObj);
|
|
|
|
const client = await this.getLargeMediaClient();
|
|
|
|
const fixedPath = path.startsWith('/') ? path.slice(1) : path;
|
|
|
|
if (!client.enabled || !client.matchPath(fixedPath)) {
|
2020-01-15 00:15:14 +02:00
|
|
|
return this.backend!.persistMedia(mediaFile, options);
|
2019-03-22 06:59:06 -07:00
|
|
|
}
|
|
|
|
|
2020-01-21 18:57:36 +02:00
|
|
|
const persistMediaArgument = await getPointerFileForMediaFileObj(client, fileObj as File, path);
|
2019-03-22 06:59:06 -07:00
|
|
|
return {
|
2020-01-15 00:15:14 +02:00
|
|
|
...(await this.backend!.persistMedia(persistMediaArgument, options)),
|
2019-03-22 06:59:06 -07:00
|
|
|
displayURL,
|
|
|
|
};
|
2018-08-07 14:46:54 -06:00
|
|
|
}
|
2020-01-15 00:15:14 +02:00
|
|
|
deleteFile(path: string, commitMessage: string) {
|
|
|
|
return this.backend!.deleteFile(path, commitMessage);
|
2018-08-07 14:46:54 -06:00
|
|
|
}
|
2020-01-15 00:15:14 +02:00
|
|
|
async getDeployPreview(collection: string, slug: string) {
|
|
|
|
return this.backend!.getDeployPreview(collection, slug);
|
2019-02-08 12:26:59 -05:00
|
|
|
}
|
2018-08-07 14:46:54 -06:00
|
|
|
unpublishedEntries() {
|
2020-01-15 00:15:14 +02:00
|
|
|
return this.backend!.unpublishedEntries();
|
2018-08-07 14:46:54 -06:00
|
|
|
}
|
2020-01-15 00:15:14 +02:00
|
|
|
unpublishedEntry(collection: string, slug: string) {
|
|
|
|
return this.backend!.unpublishedEntry(collection, slug, {
|
|
|
|
loadEntryMediaFiles: (branch, files) => this.loadEntryMediaFiles(branch, files),
|
2019-12-18 18:16:02 +02:00
|
|
|
});
|
2018-08-07 14:46:54 -06:00
|
|
|
}
|
2020-01-15 00:15:14 +02:00
|
|
|
updateUnpublishedEntryStatus(collection: string, slug: string, newStatus: string) {
|
|
|
|
return this.backend!.updateUnpublishedEntryStatus(collection, slug, newStatus);
|
2018-08-07 14:46:54 -06:00
|
|
|
}
|
2020-01-15 00:15:14 +02:00
|
|
|
deleteUnpublishedEntry(collection: string, slug: string) {
|
|
|
|
return this.backend!.deleteUnpublishedEntry(collection, slug);
|
2018-08-07 14:46:54 -06:00
|
|
|
}
|
2020-01-15 00:15:14 +02:00
|
|
|
publishUnpublishedEntry(collection: string, slug: string) {
|
|
|
|
return this.backend!.publishUnpublishedEntry(collection, slug);
|
2018-08-07 14:46:54 -06:00
|
|
|
}
|
2020-01-15 00:15:14 +02:00
|
|
|
traverseCursor(cursor: Cursor, action: string) {
|
|
|
|
return (this.backend as GitLabBackend | BitbucketBackend).traverseCursor!(cursor, action);
|
2018-08-07 14:46:54 -06:00
|
|
|
}
|
2016-12-23 16:59:48 -02:00
|
|
|
}
|