2022-07-04 14:14:55 +00:00
|
|
|
import pidusage from '@gristlabs/pidusage';
|
2022-03-24 10:59:47 +00:00
|
|
|
import {Document} from 'app/gen-server/entity/Document';
|
|
|
|
import {getScope} from 'app/server/lib/requestUtils';
|
2020-07-21 13:20:51 +00:00
|
|
|
import * as bluebird from 'bluebird';
|
|
|
|
import {EventEmitter} from 'events';
|
|
|
|
import * as path from 'path';
|
|
|
|
|
|
|
|
import {ApiError} from 'app/common/ApiError';
|
2022-05-18 16:05:37 +00:00
|
|
|
import {mapSetOrClear, MapWithTTL} from 'app/common/AsyncCreate';
|
2020-07-21 13:20:51 +00:00
|
|
|
import {BrowserSettings} from 'app/common/BrowserSettings';
|
|
|
|
import {DocCreationInfo, DocEntry, DocListAPI, OpenDocMode, OpenLocalDocResult} from 'app/common/DocListAPI';
|
2022-05-16 17:41:12 +00:00
|
|
|
import {FilteredDocUsageSummary} from 'app/common/DocUsage';
|
2020-07-21 13:20:51 +00:00
|
|
|
import {Invite} from 'app/common/sharing';
|
|
|
|
import {tbind} from 'app/common/tbind';
|
|
|
|
import {NEW_DOCUMENT_CODE} from 'app/common/UserAPI';
|
|
|
|
import {HomeDBManager} from 'app/gen-server/lib/HomeDBManager';
|
2021-07-01 15:15:43 +00:00
|
|
|
import {assertAccess, Authorizer, DocAuthorizer, DummyAuthorizer, isSingleUserMode} from 'app/server/lib/Authorizer';
|
2020-07-21 13:20:51 +00:00
|
|
|
import {Client} from 'app/server/lib/Client';
|
2021-07-01 15:15:43 +00:00
|
|
|
import {
|
|
|
|
getDocSessionCachedDoc,
|
|
|
|
makeExceptionalDocSession,
|
|
|
|
makeOptDocSession,
|
|
|
|
OptDocSession
|
|
|
|
} from 'app/server/lib/DocSession';
|
2020-07-21 13:20:51 +00:00
|
|
|
import * as docUtils from 'app/server/lib/docUtils';
|
|
|
|
import {GristServer} from 'app/server/lib/GristServer';
|
|
|
|
import {IDocStorageManager} from 'app/server/lib/IDocStorageManager';
|
|
|
|
import {makeForkIds, makeId} from 'app/server/lib/idUtils';
|
2021-07-14 11:17:12 +00:00
|
|
|
import {checkAllegedGristDoc} from 'app/server/lib/serverUtils';
|
2022-07-04 14:14:55 +00:00
|
|
|
import log from 'app/server/lib/log';
|
2020-07-21 13:20:51 +00:00
|
|
|
import {ActiveDoc} from './ActiveDoc';
|
|
|
|
import {PluginManager} from './PluginManager';
|
|
|
|
import {getFileUploadInfo, globalUploadSet, makeAccessId, UploadInfo} from './uploads';
|
2021-07-01 15:15:43 +00:00
|
|
|
import noop = require('lodash/noop');
|
2020-07-21 13:20:51 +00:00
|
|
|
|
|
|
|
// A TTL in milliseconds to use for material that can easily be recomputed / refetched
|
|
|
|
// but is a bit of a burden under heavy traffic.
|
|
|
|
export const DEFAULT_CACHE_TTL = 10000;
|
|
|
|
|
2022-05-18 16:05:37 +00:00
|
|
|
// How long to remember that a document has been explicitly set in a
|
|
|
|
// recovery mode.
|
|
|
|
export const RECOVERY_CACHE_TTL = 30000;
|
|
|
|
|
2020-07-21 13:20:51 +00:00
|
|
|
/**
|
|
|
|
* DocManager keeps track of "active" Grist documents, i.e. those loaded
|
|
|
|
* in-memory, with clients connected to them.
|
|
|
|
*/
|
|
|
|
export class DocManager extends EventEmitter {
|
|
|
|
// Maps docName to promise for ActiveDoc object. Most of the time the promise
|
|
|
|
// will be long since resolved, with the resulting document cached.
|
|
|
|
private _activeDocs: Map<string, Promise<ActiveDoc>> = new Map();
|
2022-05-18 16:05:37 +00:00
|
|
|
// Remember recovery mode of documents.
|
|
|
|
private _inRecovery = new MapWithTTL<string, boolean>(RECOVERY_CACHE_TTL);
|
2020-07-21 13:20:51 +00:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
public readonly storageManager: IDocStorageManager,
|
(core) add a `yarn run cli` tool, and add a `sqlite gristify` option
Summary:
This adds rudimentary support for opening certain SQLite files in Grist.
If you have a file such as `landing.db` in Grist, you can convert it to Grist format by doing (either in monorepo or grist-core):
```
yarn run cli -h
yarn run cli sqlite -h
yarn run cli sqlite gristify landing.db
```
The file is now openable by Grist. To actually do so with the regular Grist server, you'll need to either import it, or convert some doc you don't care about in the `samples/` directory to be a soft link to it (and then force a reload).
This implementation is a rudimentary experiment. Here are some awkwardnesses:
* Only tables that happen to have a column called `id`, and where the column happens to be an integer, can be opened directly with Grist as it is today. That could be generalized, but it looked more than a Gristathon's worth of work, so I instead used SQLite views.
* Grist will handle tables that start with an uncapitalized letter a bit erratically. You can successfully add columns, for example, but removing them will cause sadness - Grist will rename the table in a confused way.
* I didn't attempt to deal with column names with spaces etc (though views could deal with those).
* I haven't tried to do any fancy type mapping.
* Columns with constraints can make adding new rows impossible in Grist, since Grist requires that a row can be added with just a single cell set.
Test Plan: added small test
Reviewers: georgegevoian
Reviewed By: georgegevoian
Differential Revision: https://phab.getgrist.com/D3502
2022-07-14 09:32:06 +00:00
|
|
|
public readonly pluginManager: PluginManager|null,
|
2020-07-21 13:20:51 +00:00
|
|
|
private _homeDbManager: HomeDBManager|null,
|
|
|
|
public gristServer: GristServer
|
|
|
|
) {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2022-05-18 16:05:37 +00:00
|
|
|
public setRecovery(docId: string, recovery: boolean) {
|
|
|
|
this._inRecovery.set(docId, recovery);
|
|
|
|
}
|
|
|
|
|
2020-07-21 13:20:51 +00:00
|
|
|
// attach a home database to the DocManager. During some tests, it
|
|
|
|
// is awkward to have this set up at the point of construction.
|
|
|
|
public testSetHomeDbManager(dbManager: HomeDBManager) {
|
|
|
|
this._homeDbManager = dbManager;
|
|
|
|
}
|
|
|
|
|
2021-01-15 21:56:58 +00:00
|
|
|
public getHomeDbManager() {
|
|
|
|
return this._homeDbManager;
|
|
|
|
}
|
|
|
|
|
2020-07-21 13:20:51 +00:00
|
|
|
/**
|
|
|
|
* Returns an implementation of the DocListAPI for the given Client object.
|
|
|
|
*/
|
|
|
|
public getDocListAPIImpl(client: Client): DocListAPI {
|
|
|
|
return {
|
|
|
|
getDocList: tbind(this.listDocs, this, client),
|
|
|
|
createNewDoc: tbind(this.createNewDoc, this, client),
|
|
|
|
importSampleDoc: tbind(this.importSampleDoc, this, client),
|
|
|
|
importDoc: tbind(this.importDoc, this, client),
|
|
|
|
deleteDoc: tbind(this.deleteDoc, this, client),
|
|
|
|
renameDoc: tbind(this.renameDoc, this, client),
|
|
|
|
openDoc: tbind(this.openDoc, this, client),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of currently open docs.
|
|
|
|
*/
|
|
|
|
public numOpenDocs(): number {
|
|
|
|
return this._activeDocs.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a Map from docId to number of connected clients for each doc.
|
|
|
|
*/
|
|
|
|
public async getDocClientCounts(): Promise<Map<string, number>> {
|
|
|
|
const values = await Promise.all(Array.from(this._activeDocs.values(), async (adocPromise) => {
|
|
|
|
const adoc = await adocPromise;
|
|
|
|
return [adoc.docName, adoc.docClients.clientCount()] as [string, number];
|
|
|
|
}));
|
|
|
|
return new Map(values);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a promise for all known Grist documents and document invites to show in the doc list.
|
|
|
|
*/
|
|
|
|
public async listDocs(client: Client): Promise<{docs: DocEntry[], docInvites: DocEntry[]}> {
|
|
|
|
const docs = await this.storageManager.listDocs();
|
|
|
|
return {docs, docInvites: []};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a promise for invites to docs which have not been downloaded.
|
|
|
|
*/
|
|
|
|
public async getLocalInvites(client: Client): Promise<Invite[]> {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new document, fetches it, and adds a table to it.
|
|
|
|
* @returns {Promise:String} The name of the new document.
|
|
|
|
*/
|
|
|
|
public async createNewDoc(client: Client): Promise<string> {
|
|
|
|
log.debug('DocManager.createNewDoc');
|
2020-09-02 18:17:17 +00:00
|
|
|
const docSession = makeExceptionalDocSession('nascent', {client});
|
2021-01-12 15:48:40 +00:00
|
|
|
return this.createNamedDoc(docSession, 'Untitled');
|
|
|
|
}
|
|
|
|
|
|
|
|
public async createNamedDoc(docSession: OptDocSession, docId: string): Promise<string> {
|
|
|
|
const activeDoc: ActiveDoc = await this.createNewEmptyDoc(docSession, docId);
|
2020-09-02 18:17:17 +00:00
|
|
|
await activeDoc.addInitialTable(docSession);
|
2020-07-21 13:20:51 +00:00
|
|
|
return activeDoc.docName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new document, fetches it, and adds a table to it.
|
|
|
|
* @param {String} sampleDocName: Doc name of a sample document.
|
|
|
|
* @returns {Promise:String} The name of the new document.
|
|
|
|
*/
|
|
|
|
public async importSampleDoc(client: Client, sampleDocName: string): Promise<string> {
|
|
|
|
const sourcePath = this.storageManager.getSampleDocPath(sampleDocName);
|
|
|
|
if (!sourcePath) {
|
|
|
|
throw new Error(`no path available to sample ${sampleDocName}`);
|
|
|
|
}
|
|
|
|
log.info('DocManager.importSampleDoc importing', sourcePath);
|
|
|
|
const basenameHint = path.basename(sampleDocName);
|
|
|
|
const targetName = await docUtils.createNumbered(basenameHint, '-',
|
|
|
|
(name: string) => docUtils.createExclusive(this.storageManager.getPath(name)));
|
|
|
|
|
|
|
|
const targetPath = this.storageManager.getPath(targetName);
|
|
|
|
log.info('DocManager.importSampleDoc saving as', targetPath);
|
|
|
|
await docUtils.copyFile(sourcePath, targetPath);
|
|
|
|
return targetName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes an upload, containing possibly multiple files, to create a single new document, and
|
|
|
|
* returns the new document's name/id.
|
|
|
|
*/
|
|
|
|
public async importDoc(client: Client, uploadId: number): Promise<string> {
|
|
|
|
const userId = this._homeDbManager ? await client.requireUserId(this._homeDbManager) : null;
|
|
|
|
const result = await this._doImportDoc(makeOptDocSession(client),
|
|
|
|
globalUploadSet.getUploadInfo(uploadId, this.makeAccessId(userId)), {naming: 'classic'});
|
|
|
|
return result.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Import a document, assigning it a unique id distinct from its title. Cleans up uploadId.
|
|
|
|
public importDocWithFreshId(docSession: OptDocSession, userId: number, uploadId: number): Promise<DocCreationInfo> {
|
|
|
|
const accessId = this.makeAccessId(userId);
|
|
|
|
return this._doImportDoc(docSession, globalUploadSet.getUploadInfo(uploadId, accessId),
|
|
|
|
{naming: 'saved'});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do an import targeted at a specific workspace. Cleans up uploadId.
|
|
|
|
// UserId should correspond to the user making the request.
|
|
|
|
// A workspaceId of null results in an import to an unsaved doc, not
|
|
|
|
// associated with a specific workspace.
|
|
|
|
public async importDocToWorkspace(
|
|
|
|
userId: number, uploadId: number, workspaceId: number|null, browserSettings?: BrowserSettings,
|
|
|
|
): Promise<DocCreationInfo> {
|
|
|
|
if (!this._homeDbManager) { throw new Error("HomeDbManager not available"); }
|
|
|
|
|
|
|
|
const accessId = this.makeAccessId(userId);
|
2020-09-02 18:17:17 +00:00
|
|
|
const docSession = makeExceptionalDocSession('nascent', {browserSettings});
|
2021-03-18 22:40:02 +00:00
|
|
|
const register = async (docId: string, docTitle: string) => {
|
|
|
|
if (!workspaceId || !this._homeDbManager) { return; }
|
2020-07-21 13:20:51 +00:00
|
|
|
const queryResult = await this._homeDbManager.addDocument({userId}, workspaceId,
|
2021-03-18 22:40:02 +00:00
|
|
|
{name: docTitle}, docId);
|
2020-07-21 13:20:51 +00:00
|
|
|
if (queryResult.status !== 200) {
|
|
|
|
// TODO The ready-to-add document is not yet in storageManager, but is in the filesystem. It
|
|
|
|
// should get cleaned up in case of error here.
|
|
|
|
throw new ApiError(queryResult.errMessage || 'unable to add imported document', queryResult.status);
|
|
|
|
}
|
2021-03-18 22:40:02 +00:00
|
|
|
};
|
|
|
|
return this._doImportDoc(docSession,
|
|
|
|
globalUploadSet.getUploadInfo(uploadId, accessId), {
|
|
|
|
naming: workspaceId ? 'saved' : 'unsaved',
|
|
|
|
register,
|
|
|
|
userId,
|
|
|
|
});
|
2020-07-21 13:20:51 +00:00
|
|
|
|
2021-01-12 15:48:40 +00:00
|
|
|
// The imported document is associated with the worker that did the import.
|
|
|
|
// We could break that association (see /api/docs/:docId/assign for how) if
|
|
|
|
// we start using dedicated import workers.
|
2020-07-21 13:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Imports file at filepath into the app by creating a new document and adding the file to
|
|
|
|
* the documents directory.
|
|
|
|
* @param {String} filepath - Path to the current location of the file on the server.
|
|
|
|
* @returns {Promise:String} The name of the new document.
|
|
|
|
*/
|
|
|
|
public async importNewDoc(filepath: string): Promise<DocCreationInfo> {
|
|
|
|
const uploadId = globalUploadSet.registerUpload([await getFileUploadInfo(filepath)], null, noop, null);
|
|
|
|
return await this._doImportDoc(makeOptDocSession(null), globalUploadSet.getUploadInfo(uploadId, null),
|
|
|
|
{naming: 'classic'});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes the Grist files and directories for a given document name.
|
|
|
|
* @param {String} docName - The name of the Grist document to be deleted.
|
|
|
|
* @returns {Promise:String} The name of the deleted Grist document.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public async deleteDoc(client: Client|null, docName: string, deletePermanently: boolean): Promise<string> {
|
|
|
|
log.debug('DocManager.deleteDoc starting for %s', docName);
|
|
|
|
const docPromise = this._activeDocs.get(docName);
|
|
|
|
if (docPromise) {
|
|
|
|
// Call activeDoc's shutdown method first, to remove the doc from internal structures.
|
|
|
|
const doc: ActiveDoc = await docPromise;
|
|
|
|
await doc.shutdown();
|
|
|
|
}
|
|
|
|
await this.storageManager.deleteDoc(docName, deletePermanently);
|
|
|
|
return docName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Interrupt all clients, forcing them to reconnect. Handy when a document has changed
|
|
|
|
* status in some major way that affects access rights, such as being deleted.
|
|
|
|
*/
|
|
|
|
public async interruptDocClients(docName: string) {
|
|
|
|
const docPromise = this._activeDocs.get(docName);
|
|
|
|
if (docPromise) {
|
|
|
|
const doc: ActiveDoc = await docPromise;
|
|
|
|
doc.docClients.interruptAllClients();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens a document. Adds the client as a subscriber to the document, and fetches and returns the
|
|
|
|
* document's metadata.
|
|
|
|
* @returns {Promise:Object} An object with properties:
|
|
|
|
* `docFD` - the descriptor to use in further methods and messages about this document,
|
|
|
|
* `doc` - the object with metadata tables.
|
|
|
|
*/
|
|
|
|
public async openDoc(client: Client, docId: string,
|
2020-12-09 13:57:35 +00:00
|
|
|
mode: OpenDocMode = 'default',
|
|
|
|
linkParameters: Record<string, string> = {}): Promise<OpenLocalDocResult> {
|
2020-07-21 13:20:51 +00:00
|
|
|
let auth: Authorizer;
|
|
|
|
const dbManager = this._homeDbManager;
|
|
|
|
if (!isSingleUserMode()) {
|
|
|
|
if (!dbManager) { throw new Error("HomeDbManager not available"); }
|
|
|
|
// Sets up authorization of the document.
|
|
|
|
const org = client.getOrg();
|
|
|
|
if (!org) { throw new Error('Documents can only be opened in the context of a specific organization'); }
|
|
|
|
const userId = await client.getUserId(dbManager) || dbManager.getAnonymousUserId();
|
|
|
|
|
|
|
|
// We use docId in the key, and disallow urlId, so we can be sure that we are looking at the
|
|
|
|
// right doc when we re-query the DB over the life of the websocket.
|
|
|
|
const key = {urlId: docId, userId, org};
|
|
|
|
log.debug("DocManager.openDoc Authorizer key", key);
|
|
|
|
const docAuth = await dbManager.getDocAuthCached(key);
|
|
|
|
assertAccess('viewers', docAuth);
|
|
|
|
|
|
|
|
if (docAuth.docId !== docId) {
|
|
|
|
// The only plausible way to end up here is if we called openDoc with a urlId rather
|
|
|
|
// than a docId.
|
|
|
|
throw new Error(`openDoc expected docId ${docAuth.docId} not urlId ${docId}`);
|
|
|
|
}
|
2020-12-09 13:57:35 +00:00
|
|
|
auth = new DocAuthorizer(dbManager, key, mode, linkParameters, docAuth, client.getProfile() || undefined);
|
2020-07-21 13:20:51 +00:00
|
|
|
} else {
|
|
|
|
log.debug(`DocManager.openDoc not using authorization for ${docId} because GRIST_SINGLE_USER`);
|
2020-09-02 18:17:17 +00:00
|
|
|
auth = new DummyAuthorizer('owners', docId);
|
2020-07-21 13:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the document, and continue when we have the ActiveDoc (which may be immediately).
|
2020-09-02 18:17:17 +00:00
|
|
|
const docSessionPrecursor = makeOptDocSession(client);
|
|
|
|
docSessionPrecursor.authorizer = auth;
|
2020-07-21 13:20:51 +00:00
|
|
|
|
2021-11-16 21:10:57 +00:00
|
|
|
return this._withUnmutedDoc(docSessionPrecursor, docId, async () => {
|
|
|
|
const activeDoc: ActiveDoc = await this.fetchDoc(docSessionPrecursor, docId);
|
|
|
|
|
|
|
|
// Get a fresh DocSession object.
|
|
|
|
const docSession = activeDoc.addClient(client, auth);
|
|
|
|
|
|
|
|
// If opening in (pre-)fork mode, check if it is appropriate to treat the user as
|
|
|
|
// an owner for granular access purposes.
|
|
|
|
if (mode === 'fork') {
|
|
|
|
if (await activeDoc.canForkAsOwner(docSession)) {
|
|
|
|
// Mark the session specially and flush any cached access
|
|
|
|
// information. It is easier to make this a property of the
|
|
|
|
// session than to try computing it later in the heat of
|
|
|
|
// battle, since it introduces a loop where a user property
|
|
|
|
// (user.Access) depends on evaluating rules, but rules need
|
|
|
|
// the user properties in order to be evaluated. It is also
|
|
|
|
// somewhat justifiable even if permissions change later on
|
|
|
|
// the theory that the fork is theoretically happening at this
|
|
|
|
// instance).
|
|
|
|
docSession.forkingAsOwner = true;
|
|
|
|
activeDoc.flushAccess(docSession);
|
|
|
|
} else {
|
|
|
|
// TODO: it would be kind to pass on a message to the client
|
|
|
|
// to let them know they won't be able to fork. They'll get
|
|
|
|
// an error when they make their first change. But currently
|
|
|
|
// we only have the blunt instrument of throwing an error,
|
|
|
|
// which would prevent access to the document entirely.
|
|
|
|
}
|
2021-07-20 20:20:31 +00:00
|
|
|
}
|
|
|
|
|
2022-05-03 05:20:31 +00:00
|
|
|
const [metaTables, recentActions, userOverride] = await Promise.all([
|
2021-11-16 21:10:57 +00:00
|
|
|
activeDoc.fetchMetaTables(docSession),
|
2022-03-14 18:16:30 +00:00
|
|
|
activeDoc.getRecentMinimalActions(docSession),
|
|
|
|
activeDoc.getUserOverride(docSession),
|
2021-11-16 21:10:57 +00:00
|
|
|
]);
|
|
|
|
|
2022-05-16 17:41:12 +00:00
|
|
|
let docUsage: FilteredDocUsageSummary | undefined;
|
2022-05-03 05:20:31 +00:00
|
|
|
try {
|
2022-05-16 17:41:12 +00:00
|
|
|
docUsage = await activeDoc.getFilteredDocUsageSummary(docSession);
|
2022-05-03 05:20:31 +00:00
|
|
|
} catch (e) {
|
|
|
|
log.warn("DocManager.openDoc failed to get doc usage", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
const result: OpenLocalDocResult = {
|
2021-11-16 21:10:57 +00:00
|
|
|
docFD: docSession.fd,
|
|
|
|
clientId: docSession.client.clientId,
|
|
|
|
doc: metaTables,
|
|
|
|
log: recentActions,
|
|
|
|
recoveryMode: activeDoc.recoveryMode,
|
2022-03-14 18:16:30 +00:00
|
|
|
userOverride,
|
2022-05-03 05:20:31 +00:00
|
|
|
docUsage,
|
|
|
|
};
|
2021-11-16 21:10:57 +00:00
|
|
|
|
|
|
|
if (!activeDoc.muted) {
|
|
|
|
this.emit('open-doc', this.storageManager.getPath(activeDoc.docName));
|
|
|
|
}
|
2020-07-21 13:20:51 +00:00
|
|
|
|
2021-11-16 21:10:57 +00:00
|
|
|
return {activeDoc, result};
|
|
|
|
});
|
2020-07-21 13:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shut down all open docs. This is called, in particular, on server shutdown.
|
|
|
|
*/
|
|
|
|
public async shutdownAll() {
|
|
|
|
await Promise.all(Array.from(this._activeDocs.values(),
|
|
|
|
adocPromise => adocPromise.then(adoc => adoc.shutdown())));
|
|
|
|
try {
|
|
|
|
await this.storageManager.closeStorage();
|
|
|
|
} catch (err) {
|
|
|
|
log.error('DocManager had problem shutting down storage: %s', err.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the setInterval that the pidusage module sets up internally.
|
|
|
|
pidusage.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Access a document by name.
|
|
|
|
public getActiveDoc(docName: string): Promise<ActiveDoc>|undefined {
|
|
|
|
return this._activeDocs.get(docName);
|
|
|
|
}
|
|
|
|
|
2021-11-16 21:10:57 +00:00
|
|
|
public removeActiveDoc(activeDoc: ActiveDoc): void {
|
2020-07-21 13:20:51 +00:00
|
|
|
this._activeDocs.delete(activeDoc.docName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async renameDoc(client: Client, oldName: string, newName: string): Promise<void> {
|
|
|
|
log.debug('DocManager.renameDoc %s -> %s', oldName, newName);
|
|
|
|
const docPromise = this._activeDocs.get(oldName);
|
|
|
|
if (docPromise) {
|
|
|
|
const adoc: ActiveDoc = await docPromise;
|
2020-09-02 18:17:17 +00:00
|
|
|
await adoc.renameDocTo({client}, newName);
|
2020-07-21 13:20:51 +00:00
|
|
|
this._activeDocs.set(newName, docPromise);
|
|
|
|
this._activeDocs.delete(oldName);
|
|
|
|
} else {
|
|
|
|
await this.storageManager.renameDoc(oldName, newName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-30 16:53:23 +00:00
|
|
|
public markAsChanged(activeDoc: ActiveDoc, reason?: 'edit') {
|
|
|
|
// Ignore changes if document is muted or in the middle of a migration.
|
|
|
|
if (!activeDoc.muted && !activeDoc.isMigrating()) {
|
|
|
|
this.storageManager.markAsChanged(activeDoc.docName, reason);
|
2020-07-21 13:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-30 16:53:23 +00:00
|
|
|
public async makeBackup(activeDoc: ActiveDoc, name: string): Promise<string> {
|
|
|
|
if (activeDoc.muted) { throw new Error('Document is disabled'); }
|
|
|
|
return this.storageManager.makeBackup(activeDoc.docName, name);
|
2020-07-21 13:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function for creating a new empty document that also emits an event.
|
|
|
|
* @param docSession The client session.
|
|
|
|
* @param basenameHint Suggested base name to use (no directory, no extension).
|
|
|
|
*/
|
|
|
|
public async createNewEmptyDoc(docSession: OptDocSession, basenameHint: string): Promise<ActiveDoc> {
|
|
|
|
const docName = await this._createNewDoc(basenameHint);
|
|
|
|
return mapSetOrClear(this._activeDocs, docName,
|
2021-03-18 22:40:02 +00:00
|
|
|
this._createActiveDoc(docSession, docName)
|
2021-10-01 13:45:27 +00:00
|
|
|
.then(newDoc => newDoc.createEmptyDoc(docSession)));
|
2020-07-21 13:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-16 21:10:57 +00:00
|
|
|
* Fetches an ActiveDoc object. Used by openDoc. If ActiveDoc is muted (for safe closing),
|
|
|
|
* wait for another.
|
2020-07-21 13:20:51 +00:00
|
|
|
*/
|
2020-12-14 17:42:09 +00:00
|
|
|
public async fetchDoc(docSession: OptDocSession, docName: string,
|
|
|
|
wantRecoveryMode?: boolean): Promise<ActiveDoc> {
|
2020-07-21 13:20:51 +00:00
|
|
|
log.debug('DocManager.fetchDoc', docName);
|
2021-11-16 21:10:57 +00:00
|
|
|
return this._withUnmutedDoc(docSession, docName, async () => {
|
|
|
|
const activeDoc = await this._fetchPossiblyMutedDoc(docSession, docName, wantRecoveryMode);
|
|
|
|
return {activeDoc, result: activeDoc};
|
|
|
|
});
|
2020-07-21 13:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public makeAccessId(userId: number|null): string|null {
|
|
|
|
return makeAccessId(this.gristServer, userId);
|
|
|
|
}
|
|
|
|
|
2021-01-12 15:48:40 +00:00
|
|
|
public isAnonymous(userId: number): boolean {
|
|
|
|
if (!this._homeDbManager) { throw new Error("HomeDbManager not available"); }
|
|
|
|
return userId === this._homeDbManager.getAnonymousUserId();
|
|
|
|
}
|
|
|
|
|
2021-11-16 21:10:57 +00:00
|
|
|
/**
|
|
|
|
* Perform the supplied operation and return its result - unless the activeDoc it returns
|
|
|
|
* is found to be muted, in which case we retry.
|
|
|
|
*/
|
|
|
|
private async _withUnmutedDoc<T>(docSession: OptDocSession, docName: string,
|
|
|
|
op: () => Promise<{ result: T, activeDoc: ActiveDoc }>): Promise<T> {
|
|
|
|
// Repeat until we acquire an ActiveDoc that is not muted (shutting down).
|
|
|
|
for (;;) {
|
|
|
|
const { result, activeDoc } = await op();
|
|
|
|
if (!activeDoc.muted) { return result; }
|
|
|
|
log.debug('DocManager._withUnmutedDoc waiting because doc is muted', docName);
|
|
|
|
await bluebird.delay(1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Like fetchDoc(), but doesn't check if ActiveDoc returned is unmuted.
|
|
|
|
private async _fetchPossiblyMutedDoc(docSession: OptDocSession, docName: string,
|
|
|
|
wantRecoveryMode?: boolean): Promise<ActiveDoc> {
|
|
|
|
if (this._activeDocs.has(docName) && wantRecoveryMode !== undefined) {
|
|
|
|
const activeDoc = await this._activeDocs.get(docName);
|
|
|
|
if (activeDoc && activeDoc.recoveryMode !== wantRecoveryMode && await activeDoc.isOwner(docSession)) {
|
|
|
|
// shutting doc down to have a chance to re-open in the correct mode.
|
|
|
|
// TODO: there could be a battle with other users opening it in a different mode.
|
|
|
|
await activeDoc.shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let activeDoc: ActiveDoc;
|
|
|
|
if (!this._activeDocs.has(docName)) {
|
|
|
|
activeDoc = await mapSetOrClear(
|
|
|
|
this._activeDocs, docName,
|
2022-05-18 16:05:37 +00:00
|
|
|
this._createActiveDoc(docSession, docName, wantRecoveryMode ?? this._inRecovery.get(docName))
|
2021-11-16 21:10:57 +00:00
|
|
|
.then(newDoc => {
|
|
|
|
// Propagate backupMade events from newly opened activeDocs (consolidate all to DocMan)
|
|
|
|
newDoc.on('backupMade', (bakPath: string) => {
|
|
|
|
this.emit('backupMade', bakPath);
|
|
|
|
});
|
|
|
|
return newDoc.loadDoc(docSession);
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
activeDoc = await this._activeDocs.get(docName)!;
|
|
|
|
}
|
|
|
|
return activeDoc;
|
|
|
|
}
|
|
|
|
|
2022-03-24 10:59:47 +00:00
|
|
|
private async _getDoc(docSession: OptDocSession, docName: string) {
|
2021-03-18 22:40:02 +00:00
|
|
|
const cachedDoc = getDocSessionCachedDoc(docSession);
|
2022-03-24 10:59:47 +00:00
|
|
|
if (cachedDoc) {
|
|
|
|
return cachedDoc;
|
|
|
|
}
|
|
|
|
|
|
|
|
let db: HomeDBManager;
|
2021-03-18 22:40:02 +00:00
|
|
|
try {
|
2022-03-24 10:59:47 +00:00
|
|
|
// For the sake of existing tests, get the db from gristServer where it may not exist and we should give up,
|
|
|
|
// rather than using this._homeDbManager which may exist and then it turns out the document itself doesn't.
|
|
|
|
db = this.gristServer.getHomeDBManager();
|
|
|
|
} catch (e) {
|
|
|
|
if (e.message === "no db") {
|
|
|
|
return;
|
2021-03-18 22:40:02 +00:00
|
|
|
}
|
2022-03-24 10:59:47 +00:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (docSession.req) {
|
|
|
|
const scope = getScope(docSession.req);
|
|
|
|
if (scope.urlId) {
|
|
|
|
return db.getDoc(scope);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return await db.getRawDocById(docName);
|
|
|
|
}
|
|
|
|
|
2022-07-19 15:39:49 +00:00
|
|
|
private async _getDocUrls(doc: Document) {
|
2022-03-24 10:59:47 +00:00
|
|
|
try {
|
2022-07-19 15:39:49 +00:00
|
|
|
return {
|
|
|
|
docUrl: await this.gristServer.getResourceUrl(doc),
|
|
|
|
docApiUrl: await this.gristServer.getResourceUrl(doc, 'api'),
|
|
|
|
};
|
2021-03-18 22:40:02 +00:00
|
|
|
} catch (e) {
|
|
|
|
// If there is no home url, we cannot construct links. Accept this, for the benefit
|
|
|
|
// of legacy tests.
|
2022-03-24 10:59:47 +00:00
|
|
|
if (e.message !== "need APP_HOME_URL") {
|
2021-03-18 22:40:02 +00:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2022-03-24 10:59:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private async _createActiveDoc(docSession: OptDocSession, docName: string, safeMode?: boolean) {
|
|
|
|
const doc = await this._getDoc(docSession, docName);
|
|
|
|
// Get URL for document for use with SELF_HYPERLINK().
|
2022-07-19 15:39:49 +00:00
|
|
|
const docUrls = doc && await this._getDocUrls(doc);
|
|
|
|
return new ActiveDoc(this, docName, {...docUrls, safeMode, doc});
|
2020-07-21 13:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper that implements doing the actual import of an uploaded set of files to create a new
|
|
|
|
* document.
|
|
|
|
*/
|
|
|
|
private async _doImportDoc(docSession: OptDocSession, uploadInfo: UploadInfo,
|
|
|
|
options: {
|
|
|
|
naming: 'classic'|'saved'|'unsaved',
|
2021-03-18 22:40:02 +00:00
|
|
|
register?: (docId: string, docTitle: string) => Promise<void>,
|
2020-07-21 13:20:51 +00:00
|
|
|
userId?: number,
|
|
|
|
}): Promise<DocCreationInfo> {
|
|
|
|
try {
|
|
|
|
const fileCount = uploadInfo.files.length;
|
2020-08-03 22:48:38 +00:00
|
|
|
const hasGristDoc = Boolean(uploadInfo.files.find(f => extname(f.origName) === '.grist'));
|
2020-07-21 13:20:51 +00:00
|
|
|
if (hasGristDoc && fileCount > 1) {
|
|
|
|
throw new Error('Grist docs must be uploaded individually');
|
|
|
|
}
|
|
|
|
const first = uploadInfo.files[0].origName;
|
2020-08-03 22:48:38 +00:00
|
|
|
const ext = extname(first);
|
2020-07-21 13:20:51 +00:00
|
|
|
const basename = path.basename(first, ext).trim() || "Untitled upload";
|
|
|
|
let id: string;
|
|
|
|
switch (options.naming) {
|
|
|
|
case 'saved':
|
|
|
|
id = makeId();
|
|
|
|
break;
|
|
|
|
case 'unsaved': {
|
|
|
|
const {userId} = options;
|
|
|
|
if (!userId) { throw new Error('unsaved import requires userId'); }
|
|
|
|
if (!this._homeDbManager) { throw new Error("HomeDbManager not available"); }
|
|
|
|
const isAnonymous = userId === this._homeDbManager.getAnonymousUserId();
|
|
|
|
id = makeForkIds({userId, isAnonymous, trunkDocId: NEW_DOCUMENT_CODE,
|
|
|
|
trunkUrlId: NEW_DOCUMENT_CODE}).docId;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'classic':
|
|
|
|
id = basename;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error('naming mode not recognized');
|
|
|
|
}
|
2021-03-18 22:40:02 +00:00
|
|
|
await options.register?.(id, basename);
|
2020-07-21 13:20:51 +00:00
|
|
|
if (ext === '.grist') {
|
|
|
|
// If the import is a grist file, copy it to the docs directory.
|
|
|
|
// TODO: We should be skeptical of the upload file to close a possible
|
|
|
|
// security vulnerability. See https://phab.getgrist.com/T457.
|
|
|
|
const docName = await this._createNewDoc(id);
|
2021-04-26 21:54:09 +00:00
|
|
|
const docPath: string = this.storageManager.getPath(docName);
|
2021-07-14 11:17:12 +00:00
|
|
|
const srcDocPath = uploadInfo.files[0].absPath;
|
|
|
|
await checkAllegedGristDoc(docSession, srcDocPath);
|
|
|
|
await docUtils.copyFile(srcDocPath, docPath);
|
2022-03-07 14:27:43 +00:00
|
|
|
// Go ahead and claim this document. If we wanted to serve it
|
|
|
|
// from a potentially different worker, we'd call addToStorage(docName)
|
|
|
|
// instead (we used to do this). The upload should already be happening
|
|
|
|
// on a randomly assigned worker due to the special treatment of the
|
|
|
|
// 'import' assignmentId.
|
|
|
|
await this.storageManager.prepareLocalDoc(docName);
|
|
|
|
this.storageManager.markAsChanged(docName, 'edit');
|
2020-07-21 13:20:51 +00:00
|
|
|
return {title: basename, id: docName};
|
|
|
|
} else {
|
|
|
|
const doc = await this.createNewEmptyDoc(docSession, id);
|
|
|
|
await doc.oneStepImport(docSession, uploadInfo);
|
|
|
|
return {title: basename, id: doc.docName};
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
throw new ApiError(err.message, err.status || 400, {
|
|
|
|
tips: [{action: 'ask-for-help', message: 'Ask for help'}]
|
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
await globalUploadSet.cleanup(uploadInfo.uploadId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the name for a new doc, based on basenameHint.
|
|
|
|
private async _createNewDoc(basenameHint: string): Promise<string> {
|
|
|
|
const docName: string = await docUtils.createNumbered(basenameHint, '-', async (name: string) => {
|
|
|
|
if (this._activeDocs.has(name)) {
|
|
|
|
throw new Error("Existing entry in active docs for: " + name);
|
|
|
|
}
|
|
|
|
return docUtils.createExclusive(this.storageManager.getPath(name));
|
|
|
|
});
|
|
|
|
log.debug('DocManager._createNewDoc picked name', docName);
|
(core) add a `yarn run cli` tool, and add a `sqlite gristify` option
Summary:
This adds rudimentary support for opening certain SQLite files in Grist.
If you have a file such as `landing.db` in Grist, you can convert it to Grist format by doing (either in monorepo or grist-core):
```
yarn run cli -h
yarn run cli sqlite -h
yarn run cli sqlite gristify landing.db
```
The file is now openable by Grist. To actually do so with the regular Grist server, you'll need to either import it, or convert some doc you don't care about in the `samples/` directory to be a soft link to it (and then force a reload).
This implementation is a rudimentary experiment. Here are some awkwardnesses:
* Only tables that happen to have a column called `id`, and where the column happens to be an integer, can be opened directly with Grist as it is today. That could be generalized, but it looked more than a Gristathon's worth of work, so I instead used SQLite views.
* Grist will handle tables that start with an uncapitalized letter a bit erratically. You can successfully add columns, for example, but removing them will cause sadness - Grist will rename the table in a confused way.
* I didn't attempt to deal with column names with spaces etc (though views could deal with those).
* I haven't tried to do any fancy type mapping.
* Columns with constraints can make adding new rows impossible in Grist, since Grist requires that a row can be added with just a single cell set.
Test Plan: added small test
Reviewers: georgegevoian
Reviewed By: georgegevoian
Differential Revision: https://phab.getgrist.com/D3502
2022-07-14 09:32:06 +00:00
|
|
|
await this.pluginManager?.pluginsLoaded;
|
2020-07-21 13:20:51 +00:00
|
|
|
return docName;
|
|
|
|
}
|
|
|
|
}
|
2020-08-03 22:48:38 +00:00
|
|
|
|
|
|
|
// Returns the extension of fpath (from last occurrence of "." to the end of the string), even
|
|
|
|
// when the basename is empty or starts with a period.
|
|
|
|
function extname(fpath: string): string {
|
|
|
|
return path.extname("X" + fpath);
|
|
|
|
}
|