CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/pages/api/v2/projects/public-path-license.ts
Views: 687
1
/*
2
If the given public path is unlisted and has a license associated to it,
3
apply it to the given project.
4
*/
5
6
import { isValidUUID } from "@cocalc/util/misc";
7
import { associatedLicense } from "@cocalc/server/licenses/public-path";
8
import getParams from "lib/api/get-params";
9
import { restartProjectIfRunning } from "@cocalc/server/projects/control/util";
10
import addLicenseToProject from "@cocalc/server/licenses/add-to-project";
11
12
export default async function handle(req, res) {
13
const { public_path_id, project_id } = getParams(req);
14
15
try {
16
if (!isValidUUID(project_id)) {
17
throw Error("project_id must be a valid uuid");
18
}
19
if (public_path_id?.length != 40) {
20
throw Error("public_path_id must be a sha1 hash");
21
}
22
const site_license_id = await associatedLicense(public_path_id);
23
if (site_license_id) {
24
// These are the only conditions under which we would apply a license.
25
// Apply site_license_id to project_id.
26
await addLicenseToProject({ project_id, license_id: site_license_id });
27
restartProjectIfRunning(project_id);
28
}
29
30
res.json({});
31
} catch (err) {
32
res.json({ error: err.message });
33
}
34
}
35
36