forked from pryv/open-pryv.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserLocalDirectory.ts
More file actions
92 lines (79 loc) · 2.64 KB
/
userLocalDirectory.ts
File metadata and controls
92 lines (79 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* @license
* Copyright (C) Pryv https://pryv.com
* This file is part of Pryv.io and released under BSD-Clause-3 License
* Refer to LICENSE file
*/
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
/**
* ToolSet to manipulate User's local directory
*/
const path = require('path');
const fs = require('fs/promises');
const fsSync = require('fs');
const { getConfig } = require('@pryv/boiler');
export { init, ensureUserDirectory, getPathForUser, deleteUserDirectory, getBasePath, setBasePathTestOnly };
let config: any;
let basePath: any;
// temporarly set baseBath for tests;
function setBasePathTestOnly (path: any) {
basePath = path || config.get('storages:engines:sqlite:path');
}
/**
* Load config and make sure baseUserDirectory exists
* This could also handle eventual migrations
*/
async function init () {
if (basePath) return;
config = await getConfig();
const candidateBasePath = config.get('storages:engines:sqlite:path');
if (!candidateBasePath || candidateBasePath === 'REPLACE ME') {
throw new Error('storages:engines:sqlite:path is not configured (still "REPLACE ME"). Load the paths-config plugin or set an explicit path.');
}
fsSync.mkdirSync(candidateBasePath, { recursive: true });
basePath = candidateBasePath;
}
/**
* Return and **creates** the desired user path
* @param userId -- user id (cuid format)
* @param [extraPath] -- Optional, extra path
*/
async function ensureUserDirectory (userId: any, extraPath = '') {
const resultPath = getPathForUser(userId, extraPath);
await fs.mkdir(resultPath, { recursive: true });
return resultPath;
}
/**
* Return the local storage for this user. (does not create it)
* @param userId -- user id (cuid format)
* @param [extraPath] -- Optional, extra path
*/
function getPathForUser (userId: any, extraPath = '') {
if (basePath == null) {
throw new Error('Run init() first');
}
if (!userId || userId.length < 3) {
throw new Error('Invalid or too short userId: ' + userId);
}
const dir1 = userId.substr(userId.length - 1, 1); // last character of id
const dir2 = userId.substr(userId.length - 2, 1);
const dir3 = userId.substr(userId.length - 3, 1);
const resultPath = path.join(basePath, dir1, dir2, dir3, userId, extraPath);
return resultPath;
}
/**
* Delete user data folder
*
* @param userId -- user id
*/
async function deleteUserDirectory (userId: any) {
const userFolder = getPathForUser(userId);
await fs.rm(userFolder, { recursive: true, force: true });
}
function getBasePath () {
if (basePath == null) {
throw new Error('Initialize UserLocalDirectory first');
}
return basePath;
}