@dnax/core 0.77.9 → 0.78.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/app/hono.ts +5 -3
- package/app/index.ts +2 -1
- package/driver/mongo/rest.ts +162 -7
- package/index.ts +1 -2
- package/lib/asyncLocalStorage.ts +22 -3
- package/lib/media/sftp.ts +657 -103
- package/lib/media/sftp_.txt +120 -0
- package/package.json +3 -4
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { Client } from "ssh2";
|
|
2
|
+
import type { SFTPWrapper } from "ssh2";
|
|
3
|
+
import { consola } from "consola";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import chokidar from "chokidar";
|
|
6
|
+
import fs from "fs-extra";
|
|
7
|
+
import type { Collection } from "../../types";
|
|
8
|
+
const BASE_DIR = "/uploads/";
|
|
9
|
+
type ConfigType = {
|
|
10
|
+
host: string;
|
|
11
|
+
port?: number;
|
|
12
|
+
username: string;
|
|
13
|
+
password: string;
|
|
14
|
+
remoteDir: string;
|
|
15
|
+
path?: string;
|
|
16
|
+
ignorePatterns?: string[];
|
|
17
|
+
//privateKeyPath?: string;
|
|
18
|
+
};
|
|
19
|
+
class FilesystemSftpAdapter {
|
|
20
|
+
conn: Client;
|
|
21
|
+
type: "sftp";
|
|
22
|
+
config: ConfigType;
|
|
23
|
+
constructor(
|
|
24
|
+
config = {
|
|
25
|
+
port: 22,
|
|
26
|
+
} as ConfigType
|
|
27
|
+
) {
|
|
28
|
+
this.config = config;
|
|
29
|
+
this.type = "sftp";
|
|
30
|
+
this.conn = new Client();
|
|
31
|
+
|
|
32
|
+
this.connect()
|
|
33
|
+
.then((e) => {
|
|
34
|
+
e.conn.end();
|
|
35
|
+
})
|
|
36
|
+
.catch((err) => {
|
|
37
|
+
consola.error(`SFTP: Failed to connect ${this.config.host}`.red);
|
|
38
|
+
});
|
|
39
|
+
// this.conn.
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
async syncCollectionMedia(col: Collection) {
|
|
48
|
+
if (!this.config?.remoteDir) this.config.remoteDir = "/home/" + col?.slug;
|
|
49
|
+
|
|
50
|
+
let visibility = col?.media?.visibility || "public";
|
|
51
|
+
let dirToWatch = path.join(process.cwd(), BASE_DIR, col?.slug, visibility);
|
|
52
|
+
dirToWatch = path.resolve(dirToWatch);
|
|
53
|
+
const watcher = chokidar.watch(dirToWatch, {
|
|
54
|
+
persistent: true,
|
|
55
|
+
ignoreInitial: true,
|
|
56
|
+
ignored: this.config.ignorePatterns || [],
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
watcher.on("all", (event, filePath) => {
|
|
60
|
+
if (event == "change" || event == "add") {
|
|
61
|
+
//consola.info(`SFTP: ${event} ${filePath}`);
|
|
62
|
+
let remotePath = path.join(
|
|
63
|
+
this.config.remoteDir,
|
|
64
|
+
path.basename(filePath)
|
|
65
|
+
);
|
|
66
|
+
this.put(filePath, remotePath);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
connect(): Promise<{ sftp: SFTPWrapper; conn: Client }> {
|
|
72
|
+
return new Promise((resolve, reject) => {
|
|
73
|
+
this.conn
|
|
74
|
+
.on("ready", () => {
|
|
75
|
+
this.conn.sftp((err, sftp) => {
|
|
76
|
+
if (err) {
|
|
77
|
+
this.conn.end();
|
|
78
|
+
return reject(err?.message);
|
|
79
|
+
}
|
|
80
|
+
resolve({
|
|
81
|
+
sftp: sftp,
|
|
82
|
+
conn: this.conn,
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
})
|
|
86
|
+
.on("error", (err) => {
|
|
87
|
+
this.conn.end();
|
|
88
|
+
reject(err?.message)
|
|
89
|
+
})
|
|
90
|
+
.connect(this.config);
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
async put(localPath: string, remotePath: string) {
|
|
94
|
+
let { sftp, conn } = await this.connect();
|
|
95
|
+
return new Promise(async (resolve, reject) => {
|
|
96
|
+
sftp.fastPut(
|
|
97
|
+
localPath,
|
|
98
|
+
remotePath,
|
|
99
|
+
{
|
|
100
|
+
|
|
101
|
+
mode: 0o777,
|
|
102
|
+
// concurrency
|
|
103
|
+
},
|
|
104
|
+
(err: any) => {
|
|
105
|
+
conn.end();
|
|
106
|
+
if (err) {
|
|
107
|
+
console.error(err?.message || err);
|
|
108
|
+
}
|
|
109
|
+
if (err) {
|
|
110
|
+
reject(err?.message);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
resolve(true);
|
|
114
|
+
}
|
|
115
|
+
);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export { FilesystemSftpAdapter };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dnax/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.78.0",
|
|
4
4
|
"module": "index.ts",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {},
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"@types/uuid": "^11.0.0"
|
|
16
16
|
},
|
|
17
17
|
"peerDependencies": {
|
|
18
|
-
"typescript": "^5.9.
|
|
18
|
+
"typescript": "^5.9.3"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"@colors/colors": "^1.6.0",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"dot-object": "2.1.5",
|
|
34
34
|
"fs-extra": "^11.2.0",
|
|
35
35
|
"generate-unique-id": "^2.0.3",
|
|
36
|
-
"hono": "4.
|
|
36
|
+
"hono": "4.11.7",
|
|
37
37
|
"joi": "18.0.2",
|
|
38
38
|
"json-joy": "16.8.0",
|
|
39
39
|
"jsonwebtoken": "^9.0.3",
|
|
@@ -48,7 +48,6 @@
|
|
|
48
48
|
"radash": "^12.1.0",
|
|
49
49
|
"rfc6902": "^5.1.2",
|
|
50
50
|
"sharp": "^0.34.5",
|
|
51
|
-
"ssh2": "^1.17.0",
|
|
52
51
|
"ufo": "^1.5.4",
|
|
53
52
|
"urlencode": "^2.0.0",
|
|
54
53
|
"uuid": "^13.0.0"
|