import { stripIndent } from 'common-tags'; import trim from 'lodash/trim'; import trimStart from 'lodash/trimStart'; import semaphore from 'semaphore'; import { allEntriesByFolder, asyncLock, basename, blobToFileObj, CURSOR_COMPATIBILITY_SYMBOL, entriesByFiles, entriesByFolder, filterByExtension, getBlobSHA, getMediaAsBlob, getMediaDisplayURL, localForage, runWithLock, } from '../../lib/util'; import API, { API_NAME } from './API'; import AuthenticationPage from './AuthenticationPage'; import type { Semaphore } from 'semaphore'; import type { AsyncLock, Cursor } from '../../lib/util'; import type { Config, Credentials, DisplayURL, BackendEntry, BackendClass, ImplementationFile, PersistOptions, User, } from '../../interface'; import type AssetProxy from '../../valueObjects/AssetProxy'; const MAX_CONCURRENT_DOWNLOADS = 10; export default class GitLab implements BackendClass { lock: AsyncLock; api: API | null; options: { proxied: boolean; API: API | null; }; repo: string; branch: string; apiRoot: string; token: string | null; mediaFolder?: string; _mediaDisplayURLSem?: Semaphore; constructor(config: Config, options = {}) { this.options = { proxied: false, API: null, ...options, }; if ( !this.options.proxied && (config.backend.repo === null || config.backend.repo === undefined) ) { throw new Error('The GitLab backend needs a "repo" in the backend configuration.'); } this.api = this.options.API || null; this.repo = config.backend.repo || ''; this.branch = config.backend.branch || 'main'; this.apiRoot = config.backend.api_root || 'https://gitlab.com/api/v4'; this.token = ''; this.mediaFolder = config.media_folder; this.lock = asyncLock(); } isGitBackend() { return true; } async status() { const auth = (await this.api ?.user() .then(user => !!user) .catch(e => { console.warn('Failed getting GitLab user', e); return false; })) || false; return { auth: { status: auth }, api: { status: true, statusPage: '' } }; } authComponent() { return AuthenticationPage; } restoreUser(user: User) { return this.authenticate(user); } async authenticate(state: Credentials) { this.token = state.token as string; this.api = new API({ token: this.token, branch: this.branch, repo: this.repo, apiRoot: this.apiRoot, }); const user = await this.api.user(); const isCollab = await this.api.hasWriteAccess().catch((error: Error) => { error.message = stripIndent` Repo "${this.repo}" not found. Please ensure the repo information is spelled correctly. If the repo is private, make sure you're logged into a GitLab account with access. `; throw error; }); // Unauthorized user if (!isCollab) { throw new Error('Your GitLab user account does not have access to this repo.'); } // Authorized user return { ...user, login: user.username, token: state.token as string }; } async logout() { this.token = null; return; } getToken() { return Promise.resolve(this.token); } filterFile( folder: string, file: { path: string; name: string }, extension: string, depth: number, ) { // gitlab paths include the root folder const fileFolder = trim(file.path.split(folder)[1] || '/', '/'); return filterByExtension(file, extension) && fileFolder.split('/').length <= depth; } async entriesByFolder(folder: string, extension: string, depth: number) { let cursor: Cursor; const listFiles = () => this.api!.listFiles(folder, depth > 1).then(({ files, cursor: c }) => { cursor = c.mergeMeta({ folder, extension, depth }); return files.filter(file => this.filterFile(folder, file, extension, depth)); }); const files = await entriesByFolder( listFiles, this.api!.readFile.bind(this.api!), this.api!.readFileMetadata.bind(this.api), API_NAME, ); // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore files[CURSOR_COMPATIBILITY_SYMBOL] = cursor; return files; } async listAllFiles(folder: string, extension: string, depth: number) { const files = await this.api!.listAllFiles(folder, depth > 1); const filtered = files.filter(file => this.filterFile(folder, file, extension, depth)); return filtered; } async allEntriesByFolder(folder: string, extension: string, depth: number) { const files = await allEntriesByFolder({ listAllFiles: () => this.listAllFiles(folder, extension, depth), readFile: this.api!.readFile.bind(this.api!), readFileMetadata: this.api!.readFileMetadata.bind(this.api), apiName: API_NAME, branch: this.branch, localForage, folder, extension, depth, getDefaultBranch: () => this.api!.getDefaultBranch().then(b => ({ name: b.name, sha: b.commit.id })), isShaExistsInBranch: this.api!.isShaExistsInBranch.bind(this.api!), getDifferences: (to, from) => this.api!.getDifferences(to, from), getFileId: path => this.api!.getFileId(path, this.branch), filterFile: file => this.filterFile(folder, file, extension, depth), customFetch: undefined, }); return files; } entriesByFiles(files: ImplementationFile[]) { return entriesByFiles( files, this.api!.readFile.bind(this.api!), this.api!.readFileMetadata.bind(this.api), API_NAME, ); } // Fetches a single entry. getEntry(path: string) { return this.api!.readFile(path).then(data => ({ file: { path, id: null }, data: data as string, })); } async getMedia(mediaFolder = this.mediaFolder) { if (!mediaFolder) { return []; } return this.api!.listAllFiles(mediaFolder).then(files => files.map(({ id, name, path }) => { return { id, name, path, displayURL: { id, name, path } }; }), ); } getMediaDisplayURL(displayURL: DisplayURL) { this._mediaDisplayURLSem = this._mediaDisplayURLSem || semaphore(MAX_CONCURRENT_DOWNLOADS); return getMediaDisplayURL( displayURL, this.api!.readFile.bind(this.api!), this._mediaDisplayURLSem, ); } async getMediaFile(path: string) { const name = basename(path); const blob = await getMediaAsBlob(path, null, this.api!.readFile.bind(this.api!)); const fileObj = blobToFileObj(name, blob); const url = URL.createObjectURL(fileObj); const id = await getBlobSHA(blob); return { id, displayURL: url, path, name, size: fileObj.size, file: fileObj, url, }; } async persistEntry(entry: BackendEntry, options: PersistOptions) { // persistEntry is a transactional operation return runWithLock( this.lock, () => this.api!.persistFiles(entry.dataFiles, entry.assets, options), 'Failed to acquire persist entry lock', ); } async persistMedia(mediaFile: AssetProxy, options: PersistOptions) { const fileObj = mediaFile.fileObj as File; const [id] = await Promise.all([ getBlobSHA(fileObj), this.api!.persistFiles([], [mediaFile], options), ]); const { path } = mediaFile; const url = URL.createObjectURL(fileObj); return { displayURL: url, path: trimStart(path, '/'), name: fileObj!.name, size: fileObj!.size, file: fileObj, url, id, }; } deleteFiles(paths: string[], commitMessage: string) { return this.api!.deleteFiles(paths, commitMessage); } traverseCursor(cursor: Cursor, action: string) { return this.api!.traverseCursor(cursor, action).then(async ({ entries, cursor: newCursor }) => { const [folder, depth, extension] = [ cursor.meta?.folder as string, cursor.meta?.depth as number, cursor.meta?.extension as string, ]; if (folder && depth && extension) { entries = entries.filter(f => this.filterFile(folder, f, extension, depth)); newCursor = newCursor.mergeMeta({ folder, extension, depth }); } const entriesWithData = await entriesByFiles( entries, this.api!.readFile.bind(this.api!), this.api!.readFileMetadata.bind(this.api)!, API_NAME, ); return { entries: entriesWithData, cursor: newCursor, }; }); } }