@rpcbase/server 0.380.0 → 0.382.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/package.json +16 -72
- package/src/getDerivedKey.ts +20 -0
- package/src/hashPassword.ts +24 -0
- package/src/index.ts +3 -0
- package/src/initServer.ts +68 -0
- package/src/types/index.ts +7 -0
- package/src/types/session.d.ts +10 -0
- package/boot/server.js +0 -36
- package/boot/shared.js +0 -17
- package/boot/worker.js +0 -37
- package/constants/keys.ts +0 -1
- package/database.js +0 -96
- package/express/custom_cors.js +0 -80
- package/express/dev_save_coverage.js +0 -18
- package/express/index.js +0 -93
- package/express/setup_handlers.js +0 -49
- package/files.ts +0 -1
- package/firebase.js +0 -33
- package/get_object_id.ts +0 -39
- package/index.js +0 -17
- package/mailer/index.js +0 -31
- package/mongoose/index.ts +0 -16
- package/mongoose/plugins/disable_default_timestamps_plugin.ts +0 -5
- package/mongoose/plugins/disable_default_version_key_plugin.ts +0 -5
- package/mongoose/plugins/object_id_plugin.ts +0 -31
- package/openai.js +0 -10
- package/publish-output.txt +0 -0
- package/queue/dispatch_indexer_queue.js +0 -22
- package/queue/dispatch_worker_queue.js +0 -38
- package/queue/index.js +0 -110
- package/queue/register_queue_listener.js +0 -180
- package/redis.js +0 -2
- package/rts/index.js +0 -444
- package/search/constants.ts +0 -1
- package/search/ensure_index.ts +0 -53
- package/search/get_client.ts +0 -15
- package/search/index.ts +0 -3
- package/src/access-control/apply_policies.js +0 -104
- package/src/access-control/get_added_fields.js +0 -23
- package/src/access-control/get_policies.js +0 -29
- package/src/access-control/hooks/doc_pre_create.js +0 -26
- package/src/access-control/hooks/query_pre_delete.js +0 -30
- package/src/access-control/index.js +0 -6
- package/src/access-control/mongoose_plugin.js +0 -136
- package/src/api/index.js +0 -6
- package/src/api/stored-values/get_stored_values.js +0 -41
- package/src/api/stored-values/index.js +0 -8
- package/src/api/stored-values/set_stored_values.js +0 -31
- package/src/auth/check_session.js +0 -43
- package/src/auth/forgot_password_email.html +0 -515
- package/src/auth/get_account.js +0 -35
- package/src/auth/get_accounts.js +0 -42
- package/src/auth/index.js +0 -24
- package/src/auth/reset_password.js +0 -70
- package/src/auth/set_new_password.js +0 -63
- package/src/auth/set_new_password_email.html +0 -3
- package/src/auth/sign_in.js +0 -61
- package/src/auth/sign_out.js +0 -11
- package/src/auth/sign_up.js +0 -56
- package/src/client/client_router.js +0 -105
- package/src/files/constants.ts +0 -9
- package/src/files/finalize_file_upload.ts +0 -25
- package/src/files/helpers/get_grid_fs_bucket.ts +0 -20
- package/src/files/index.js +0 -5
- package/src/files/tasks/finalize_file_upload/apply_img_preview.ts +0 -49
- package/src/files/tasks/finalize_file_upload/constants.ts +0 -23
- package/src/files/tasks/finalize_file_upload/download_file.ts +0 -98
- package/src/files/tasks/finalize_file_upload/get_text_vectors.ts +0 -13
- package/src/files/tasks/finalize_file_upload/helpers/convert_pdf_to_png.ts +0 -34
- package/src/files/tasks/finalize_file_upload/helpers/exec.ts +0 -5
- package/src/files/tasks/finalize_file_upload/helpers/get_metadata.ts +0 -18
- package/src/files/tasks/finalize_file_upload/index.ts +0 -53
- package/src/files/tasks/finalize_file_upload/run_ocr.ts +0 -42
- package/src/files/tasks/index.ts +0 -6
- package/src/files/upload_chunk.ts +0 -83
- package/src/helpers/sim_test_inject.ts +0 -21
- package/src/models/Invite.js +0 -23
- package/src/models/Notification.js +0 -44
- package/src/models/Policy.ts +0 -13
- package/src/models/ResetPasswordToken.js +0 -14
- package/src/models/SearchHistory.ts +0 -22
- package/src/models/User.js +0 -42
- package/src/models/UserStoredValues.js +0 -18
- package/src/models/index.js +0 -7
- package/src/notitications/ack_notification.js +0 -26
- package/src/notitications/get_notifications.js +0 -39
- package/src/notitications/llt/README.md +0 -8
- package/src/notitications/llt/get_llts.js +0 -42
- package/src/notitications/set_seen.js +0 -26
- package/src/sessions/index.js +0 -27
- package/src/sessions/session_proxy_middleware.js +0 -18
- package/src/sessions/session_store_middleware.js +0 -106
- package/src/sessions/warning_proxy_middleware.js +0 -17
- package/src/tasks/index.js +0 -8
- package/src/tasks/index_item.js +0 -8
- package/store/index.js +0 -31
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
/* @flow */
|
|
2
|
-
const _template = require("lodash/template")
|
|
3
|
-
const fs = require("fs")
|
|
4
|
-
const path = require("path")
|
|
5
|
-
const isEmail = require("validator/lib/isEmail")
|
|
6
|
-
|
|
7
|
-
const {hash_password, compare_hash} = require("@rpcbase/std/crypto/hash")
|
|
8
|
-
|
|
9
|
-
const mailer = require("../../mailer")
|
|
10
|
-
const mongoose = require("../../mongoose")
|
|
11
|
-
|
|
12
|
-
const set_new_password_email = require("./set_new_password_email.html")
|
|
13
|
-
|
|
14
|
-
const ResetPasswordToken = require("../models/ResetPasswordToken")
|
|
15
|
-
const User = require("../models/User")
|
|
16
|
-
|
|
17
|
-
const {APP_DOMAIN} = process.env
|
|
18
|
-
|
|
19
|
-
const email_tpl = _template(set_new_password_email)
|
|
20
|
-
|
|
21
|
-
const set_new_password = async({user_id, token, password}, ctx) => {
|
|
22
|
-
|
|
23
|
-
const reset_tokens = await ResetPasswordToken.find({user_id})
|
|
24
|
-
.limit(100)
|
|
25
|
-
|
|
26
|
-
if (reset_tokens.length === 0) {
|
|
27
|
-
return {
|
|
28
|
-
message: "Unable to validate reset token, please try again in a moment"
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
let is_match = false
|
|
33
|
-
|
|
34
|
-
for (let i = 0; i < reset_tokens.length; i++) {
|
|
35
|
-
const op = await compare_hash(token, reset_tokens[i].token_hash)
|
|
36
|
-
if (op) {
|
|
37
|
-
is_match = true
|
|
38
|
-
break
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (!is_match) {
|
|
43
|
-
return {
|
|
44
|
-
message: "Invalid or expired token, please try again in a moment"
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// token is valid, update the user password
|
|
49
|
-
const user = await User.findOne({_id: user_id}, null, {ctx})
|
|
50
|
-
const hashed_password = await hash_password(password)
|
|
51
|
-
user.password_hash = hashed_password
|
|
52
|
-
|
|
53
|
-
await user.save()
|
|
54
|
-
|
|
55
|
-
// WARNING:
|
|
56
|
-
// TODO: important! notify the user that their password has been reset
|
|
57
|
-
|
|
58
|
-
return {
|
|
59
|
-
status: "ok"
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
module.exports = set_new_password
|
package/src/auth/sign_in.js
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
/* @flow */
|
|
2
|
-
const {compare_hash} = require("@rpcbase/std/crypto/hash")
|
|
3
|
-
const debug = require("debug")
|
|
4
|
-
|
|
5
|
-
const mongoose = require("../../mongoose")
|
|
6
|
-
const User = require("../models/User")
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const log = debug("rb:auth:signin")
|
|
10
|
-
|
|
11
|
-
const has_session_store = process.env.RB_SESSION_STORE === "yes"
|
|
12
|
-
|
|
13
|
-
const USER_ID_HEADER = "rb-user-id"
|
|
14
|
-
|
|
15
|
-
const fail = () => ({
|
|
16
|
-
errors: {form: "Invalid email or password"}
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const sign_in = async({email, password}, ctx) => {
|
|
21
|
-
const {req, res} = ctx
|
|
22
|
-
|
|
23
|
-
// find matching user
|
|
24
|
-
const user = await User.findOne({email}, null)
|
|
25
|
-
|
|
26
|
-
if (!user) {
|
|
27
|
-
return fail()
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const hashed_pass = user.password_hash
|
|
31
|
-
|
|
32
|
-
const is_match = await compare_hash(password, hashed_pass)
|
|
33
|
-
|
|
34
|
-
if (is_match) {
|
|
35
|
-
log("compare_hash is match")
|
|
36
|
-
const user_id = user._id.toString()
|
|
37
|
-
|
|
38
|
-
if (has_session_store) {
|
|
39
|
-
req.session.user_id = user_id
|
|
40
|
-
req.session.signed_in_at_ms = Date.now()
|
|
41
|
-
await req.session.save()
|
|
42
|
-
} else {
|
|
43
|
-
// On sign in success, we set this header to tell the reverse-proxy sign in was OK
|
|
44
|
-
res.setHeader(USER_ID_HEADER, user_id)
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
log("session saved, user is signed in")
|
|
48
|
-
|
|
49
|
-
return {
|
|
50
|
-
status: "ok",
|
|
51
|
-
user_id,
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
} else {
|
|
55
|
-
log("no match, returning error")
|
|
56
|
-
|
|
57
|
-
return fail()
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
module.exports = sign_in
|
package/src/auth/sign_out.js
DELETED
package/src/auth/sign_up.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
/* @flow */
|
|
2
|
-
const {hash_password} = require("@rpcbase/std/crypto/hash")
|
|
3
|
-
|
|
4
|
-
const UserStoredValues = require("../models/UserStoredValues")
|
|
5
|
-
const User = require("../models/User")
|
|
6
|
-
const Invite = require("../models/Invite")
|
|
7
|
-
|
|
8
|
-
const MIN_PASSWORD_LENGTH = 12
|
|
9
|
-
|
|
10
|
-
const sign_up = async(payload, ctx, session) => {
|
|
11
|
-
const {email, password} = payload
|
|
12
|
-
expect(email).toBeEmail()
|
|
13
|
-
expect(password.length).toBeGreaterThanOrEqual(MIN_PASSWORD_LENGTH)
|
|
14
|
-
|
|
15
|
-
// check if the user already exists
|
|
16
|
-
const existing_user = await User.findOne({email}, null, {ctx, session})
|
|
17
|
-
|
|
18
|
-
if (existing_user) {
|
|
19
|
-
throw new Error("User already exists")
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// check if we have an invite for this user
|
|
23
|
-
const invite = await Invite.findOne({email}, null, {ctx, session})
|
|
24
|
-
|
|
25
|
-
// TODO: mark invite as accepted here so it can't be reused
|
|
26
|
-
if (invite && !invite.is_ready) {
|
|
27
|
-
console.log("found an invite, but not ready", email)
|
|
28
|
-
throw new Error("Your invite is still pending approval. Expect an email in the next weeks to activate your account.")
|
|
29
|
-
} else if (!invite) {
|
|
30
|
-
console.log("no invite for signup email:", email)
|
|
31
|
-
throw new Error("No invite was found for this email")
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const hash = await hash_password(password)
|
|
35
|
-
|
|
36
|
-
const user = new User({
|
|
37
|
-
email,
|
|
38
|
-
password_hash: hash
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
// sign the user in
|
|
43
|
-
const user_id = user._id.toString()
|
|
44
|
-
|
|
45
|
-
const values_store = new UserStoredValues({
|
|
46
|
-
_owners: [user_id]
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
// WARNING: it is now the responsibility of the app to add user.save with session
|
|
50
|
-
// await user.save({ctx, session})
|
|
51
|
-
// await values_store.save({ctx, session})
|
|
52
|
-
|
|
53
|
-
return {user, values_store}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
module.exports = sign_up
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
/* @flow */
|
|
2
|
-
const assert = require("assert")
|
|
3
|
-
const fs = require("fs")
|
|
4
|
-
const path = require("path")
|
|
5
|
-
const glob = require("glob11")
|
|
6
|
-
const Sentry = require("@sentry/node")
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const MIME_TYPES = {
|
|
10
|
-
".css": "text/css",
|
|
11
|
-
".js": "text/javascript",
|
|
12
|
-
".json": "application/json",
|
|
13
|
-
".svg": "image/svg+xml",
|
|
14
|
-
".woff2": "font/woff2",
|
|
15
|
-
".png": "image/png",
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const {SENTRY_DSN, NODE_ENV} = process.env
|
|
19
|
-
const has_sentry = !!SENTRY_DSN
|
|
20
|
-
|
|
21
|
-
const is_development = NODE_ENV === "development"
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const build_dir = path.join(process.cwd(), "build/")
|
|
25
|
-
const client_build_dir = path.join(build_dir, "./client")
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const get_client_routes = () => {
|
|
29
|
-
const client_files = glob.sync(path.join(client_build_dir, "./**/*"))
|
|
30
|
-
const routes = client_files
|
|
31
|
-
.filter((f) => {
|
|
32
|
-
const stat = fs.statSync(f)
|
|
33
|
-
return !stat.isDirectory()
|
|
34
|
-
})
|
|
35
|
-
.map((f) => `/${path.relative(client_build_dir, f)}`)
|
|
36
|
-
return routes
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const serve_file = (req, res, full_path) => {
|
|
41
|
-
const file_path = path.join(client_build_dir, full_path)
|
|
42
|
-
|
|
43
|
-
const extname = path.extname(file_path)
|
|
44
|
-
|
|
45
|
-
const content_type = MIME_TYPES[extname]
|
|
46
|
-
assert(content_type, "unable to resolve content type")
|
|
47
|
-
|
|
48
|
-
res.sendFile(file_path, {
|
|
49
|
-
headers: {
|
|
50
|
-
"Cache-Control": `public, max-age=1800`, // 30 mins
|
|
51
|
-
"Content-Type": content_type,
|
|
52
|
-
},
|
|
53
|
-
})
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
const client_router = (app) => {
|
|
58
|
-
const client_routes = get_client_routes()
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
const index_file_path = path.join(client_build_dir, "./index.html")
|
|
62
|
-
if (fs.existsSync(index_file_path)) {
|
|
63
|
-
const index_file_buffer = fs.readFileSync(index_file_path)
|
|
64
|
-
|
|
65
|
-
app.get("*", async(req, res, next) => {
|
|
66
|
-
const full_path = req.baseUrl + req.path
|
|
67
|
-
|
|
68
|
-
// TODO: this shouldn't be here,
|
|
69
|
-
// it should be handled by the api and rpc routers being declared before this router
|
|
70
|
-
if (full_path.startsWith("/api/") || full_path.startsWith("/rpc/")) {
|
|
71
|
-
return next()
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (client_routes.indexOf(full_path) > -1) {
|
|
75
|
-
serve_file(req, res, full_path)
|
|
76
|
-
return
|
|
77
|
-
} else {
|
|
78
|
-
// TODO: should handle 404 here
|
|
79
|
-
res.writeHead(200, {
|
|
80
|
-
"Content-Type": "text/html",
|
|
81
|
-
})
|
|
82
|
-
res.end(index_file_buffer)
|
|
83
|
-
}
|
|
84
|
-
})
|
|
85
|
-
}
|
|
86
|
-
// register final error handlers
|
|
87
|
-
|
|
88
|
-
if (has_sentry && !is_development) {
|
|
89
|
-
// The error handler must be registered before any other error middleware and after all controllers
|
|
90
|
-
app.use(Sentry.Handlers.errorHandler())
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
// TODO: this should not be here
|
|
95
|
-
app.use((err, req, res, next) => {
|
|
96
|
-
console.error("server got err2", err)
|
|
97
|
-
console.log("stack:", err.stack)
|
|
98
|
-
res.status(500).json({
|
|
99
|
-
status: "error",
|
|
100
|
-
message: "something went wrong",
|
|
101
|
-
})
|
|
102
|
-
})
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
module.exports = client_router
|
package/src/files/constants.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import Promise from "bluebird"
|
|
2
|
-
|
|
3
|
-
import queue from "../../queue"
|
|
4
|
-
import {ProcessFilePayload} from "./constants"
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const DEFAULTS = {
|
|
8
|
-
ocr: false,
|
|
9
|
-
text_vectors: false,
|
|
10
|
-
img_preview: false,
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
// after we've uploaded a file, we process it with ocr, llm vectorization, png rendering, etc
|
|
14
|
-
export const finalize_file_upload = async(files: Array<ProcessFilePayload>) => {
|
|
15
|
-
|
|
16
|
-
await Promise.map(files,
|
|
17
|
-
async(file: ProcessFilePayload) => {
|
|
18
|
-
await queue.add("finalize_file_upload", {...DEFAULTS, ...file}, {
|
|
19
|
-
jobId: `finalize_file_upload-${file.hash}`,
|
|
20
|
-
removeOnComplete: true,
|
|
21
|
-
removeOnFail: true,
|
|
22
|
-
})
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import assert from "assert"
|
|
2
|
-
import mongoose from "../../../mongoose"
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const CHUNK_SIZE = 1024 * 1024
|
|
6
|
-
|
|
7
|
-
export const get_grid_fs_bucket = (
|
|
8
|
-
bucket_name: string,
|
|
9
|
-
chunk_size: number = CHUNK_SIZE,
|
|
10
|
-
) => {
|
|
11
|
-
assert(chunk_size === CHUNK_SIZE, "chunk_size must match default CHUNK_SIZE")
|
|
12
|
-
|
|
13
|
-
const {db} = mongoose.connection
|
|
14
|
-
const bucket = new mongoose.mongo.GridFSBucket(db, {
|
|
15
|
-
bucketName: bucket_name,
|
|
16
|
-
chunkSizeBytes: chunk_size,
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
return bucket
|
|
20
|
-
}
|
package/src/files/index.js
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import {DOC_MIME_TYPES} from "./constants"
|
|
2
|
-
import {exec} from "./helpers/exec"
|
|
3
|
-
import {convert_pdf_to_png} from "./helpers/convert_pdf_to_png"
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const convert_pdf = async({tmp_wd, full_path, metadata}) => {
|
|
7
|
-
console.log("converting from:", metadata.mime_type, "to: pdf")
|
|
8
|
-
const cmd = `libreoffice --headless --convert-to pdf:writer_pdf_Export --outdir ${tmp_wd} ${full_path}`
|
|
9
|
-
const result = await exec(cmd)
|
|
10
|
-
|
|
11
|
-
if (result.stderr) {
|
|
12
|
-
console.warn(result.stderr)
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
if (result.stdout) {
|
|
16
|
-
console.log(result.stdout)
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
export const apply_img_preview = async({tmp_wd, full_path, metadata}): Promise<any> => {
|
|
22
|
-
|
|
23
|
-
let is_pdf_converted = false
|
|
24
|
-
|
|
25
|
-
// DOC
|
|
26
|
-
if (DOC_MIME_TYPES.includes(metadata.mime_type)) {
|
|
27
|
-
await convert_pdf({tmp_wd, full_path, metadata})
|
|
28
|
-
is_pdf_converted = true
|
|
29
|
-
}
|
|
30
|
-
// PDF
|
|
31
|
-
else if (metadata.mime_type === "application/pdf") {
|
|
32
|
-
// file is already a pdf, do nothing
|
|
33
|
-
is_pdf_converted = true
|
|
34
|
-
} else {
|
|
35
|
-
console.log("apply img, unknown mime type, not proceeding")
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (is_pdf_converted) {
|
|
39
|
-
const pdf_path = `${full_path}.pdf`
|
|
40
|
-
await convert_pdf_to_png({tmp_wd, pdf_path})
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// TODO: convert spreadsheets to csv ?
|
|
44
|
-
// convert powerpoints to pdf also (they should work just the same as docs)
|
|
45
|
-
|
|
46
|
-
return {
|
|
47
|
-
is_pdf_converted,
|
|
48
|
-
}
|
|
49
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
export const PDF_IMG_DIR = "img"
|
|
2
|
-
|
|
3
|
-
export const DOC_MIME_TYPES = [
|
|
4
|
-
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
5
|
-
"application/msword",
|
|
6
|
-
"application/vnd.oasis.opendocument.text",
|
|
7
|
-
"application/rtf",
|
|
8
|
-
]
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export const IMAGES_MIME_TYPES = [
|
|
12
|
-
"image/jpeg",
|
|
13
|
-
"image/png",
|
|
14
|
-
"image/gif",
|
|
15
|
-
"image/bmp",
|
|
16
|
-
"image/tiff",
|
|
17
|
-
"image/webp",
|
|
18
|
-
"image/svg+xml",
|
|
19
|
-
"image/vnd.microsoft.icon",
|
|
20
|
-
"image/heif",
|
|
21
|
-
"image/heic",
|
|
22
|
-
"image/vnd.adobe.photoshop",
|
|
23
|
-
]
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import fs from "fs/promises"
|
|
2
|
-
import {createReadStream, createWriteStream} from "fs"
|
|
3
|
-
import path from "path"
|
|
4
|
-
import {createBrotliDecompress} from "zlib"
|
|
5
|
-
import Promise from "bluebird"
|
|
6
|
-
|
|
7
|
-
import mongoose from "../../../../mongoose"
|
|
8
|
-
import {UPLOAD_BUCKET_NAME} from "../../constants"
|
|
9
|
-
import {get_grid_fs_bucket} from "../../helpers/get_grid_fs_bucket"
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const DL_CHUNK_CONCURRENCY = 8
|
|
13
|
-
|
|
14
|
-
const download_chunk =
|
|
15
|
-
(bucket: mongoose.mongo.GridFSBucket, tmp_wd: string, metadata: any) =>
|
|
16
|
-
(chunk_num: number) =>
|
|
17
|
-
new Promise((resolve, reject) => {
|
|
18
|
-
const filename = `${metadata.hash}.${chunk_num}`
|
|
19
|
-
|
|
20
|
-
const dl_stream = bucket.openDownloadStreamByName(filename)
|
|
21
|
-
|
|
22
|
-
const file_path = path.join(tmp_wd, `./${filename}`)
|
|
23
|
-
|
|
24
|
-
const write_stream = createWriteStream(file_path)
|
|
25
|
-
|
|
26
|
-
if (metadata.is_compressed) {
|
|
27
|
-
const decompress_stream = createBrotliDecompress()
|
|
28
|
-
dl_stream.pipe(decompress_stream).pipe(write_stream)
|
|
29
|
-
} else {
|
|
30
|
-
dl_stream.pipe(write_stream)
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
dl_stream.on("error", (error) => {
|
|
34
|
-
console.error("Error downloading file:", error)
|
|
35
|
-
reject(error)
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
write_stream.on("error", (error) => {
|
|
39
|
-
console.error("Error writing file:", error)
|
|
40
|
-
reject(error)
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
write_stream.on("finish", () => {
|
|
44
|
-
console.log("File downloaded and written successfully", file_path)
|
|
45
|
-
resolve()
|
|
46
|
-
})
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const rebuild_file_from_chunks = async(
|
|
51
|
-
tmp_wd: string,
|
|
52
|
-
metadata: any,
|
|
53
|
-
chunks: Array<number>,
|
|
54
|
-
) => {
|
|
55
|
-
const full_file_path = path.join(tmp_wd, `./${metadata.hash}`)
|
|
56
|
-
const output_stream = createWriteStream(full_file_path)
|
|
57
|
-
|
|
58
|
-
const append_chunk = (chunk_num: number) => new Promise<void>((resolve, reject) => {
|
|
59
|
-
const chunk_path = path.join(tmp_wd, `./${metadata.hash}.${chunk_num}`)
|
|
60
|
-
|
|
61
|
-
const chunk_read_stream = createReadStream(chunk_path)
|
|
62
|
-
|
|
63
|
-
chunk_read_stream.pipe(output_stream, {end: false})
|
|
64
|
-
|
|
65
|
-
chunk_read_stream.on("end", async() => {
|
|
66
|
-
await fs.rm(chunk_path)
|
|
67
|
-
resolve()
|
|
68
|
-
})
|
|
69
|
-
chunk_read_stream.on("error", reject)
|
|
70
|
-
})
|
|
71
|
-
|
|
72
|
-
for (const chunk_num of chunks) {
|
|
73
|
-
await append_chunk(chunk_num)
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
output_stream.end()
|
|
77
|
-
|
|
78
|
-
return full_file_path
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// downloads all chunks, reconstruct if necessary
|
|
82
|
-
export const download_file = async({tmp_wd, metadata}:
|
|
83
|
-
{ tmp_wd: string, metadata: any }): Promise<string> => {
|
|
84
|
-
const bucket = get_grid_fs_bucket(UPLOAD_BUCKET_NAME)
|
|
85
|
-
|
|
86
|
-
const chunks = Array.from({length: metadata.total_chunks}, (_, i) => i)
|
|
87
|
-
|
|
88
|
-
// download all file chunks
|
|
89
|
-
await Promise.map(
|
|
90
|
-
chunks,
|
|
91
|
-
download_chunk(bucket, tmp_wd, metadata),
|
|
92
|
-
{concurrency: DL_CHUNK_CONCURRENCY},
|
|
93
|
-
)
|
|
94
|
-
|
|
95
|
-
const full_path = await rebuild_file_from_chunks(tmp_wd, metadata, chunks)
|
|
96
|
-
|
|
97
|
-
return full_path
|
|
98
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import path from "path"
|
|
2
|
-
import {mkdirp} from "mkdirp"
|
|
3
|
-
import {fromPath} from "pdf2pic"
|
|
4
|
-
|
|
5
|
-
import {PDF_IMG_DIR} from "../constants"
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export const convert_pdf_to_png = async(
|
|
9
|
-
{tmp_wd, pdf_path}:
|
|
10
|
-
{ tmp_wd: string, pdf_path: string }
|
|
11
|
-
) =>
|
|
12
|
-
new Promise(async(resolve, reject) => {
|
|
13
|
-
const out_dir = path.join(tmp_wd, `./${PDF_IMG_DIR}/`)
|
|
14
|
-
|
|
15
|
-
await mkdirp(out_dir)
|
|
16
|
-
|
|
17
|
-
const options = {
|
|
18
|
-
density: 220,
|
|
19
|
-
saveFilename: "page",
|
|
20
|
-
savePath: out_dir,
|
|
21
|
-
format: "png",
|
|
22
|
-
// width
|
|
23
|
-
// height
|
|
24
|
-
preserveAspectRatio: true,
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const converter = fromPath(pdf_path, options)
|
|
28
|
-
|
|
29
|
-
converter.bulk(-1, {responseType: "image"})
|
|
30
|
-
.then((pages) => {
|
|
31
|
-
resolve(pages)
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
})
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import mongoose from "../../../../../mongoose"
|
|
2
|
-
|
|
3
|
-
import {UPLOAD_BUCKET_NAME} from "../../../constants"
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export const get_metadata = async(hash: string) => {
|
|
7
|
-
const files_coll = mongoose.connection.db.collection(
|
|
8
|
-
`${UPLOAD_BUCKET_NAME}.files`,
|
|
9
|
-
)
|
|
10
|
-
|
|
11
|
-
const first_chunk_name = `${hash}.0`
|
|
12
|
-
|
|
13
|
-
const file_metadata = await files_coll.findOne({
|
|
14
|
-
filename: first_chunk_name,
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
return file_metadata.metadata
|
|
18
|
-
}
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import path from "path"
|
|
2
|
-
import os from "os"
|
|
3
|
-
import {mkdirp} from "mkdirp"
|
|
4
|
-
|
|
5
|
-
import {TMP_UPLOADS_DIR} from "../../constants"
|
|
6
|
-
|
|
7
|
-
import {apply_img_preview} from "./apply_img_preview"
|
|
8
|
-
import {download_file} from "./download_file"
|
|
9
|
-
import {run_ocr} from "./run_ocr"
|
|
10
|
-
import {get_metadata} from "./helpers/get_metadata"
|
|
11
|
-
import {get_text_vectors} from "./get_text_vectors"
|
|
12
|
-
|
|
13
|
-
import {ProcessFilePayload} from "../../constants"
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const {RB_TENANT_ID} = process.env
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
export const finalize_file_upload = async(payload: ProcessFilePayload) => {
|
|
20
|
-
const {hash, img_preview, ocr, text_vectors} = payload
|
|
21
|
-
|
|
22
|
-
const tmp_wd = path.join(
|
|
23
|
-
os.tmpdir(),
|
|
24
|
-
`./${TMP_UPLOADS_DIR}/${RB_TENANT_ID}/${hash}`,
|
|
25
|
-
)
|
|
26
|
-
|
|
27
|
-
await mkdirp(tmp_wd)
|
|
28
|
-
console.log("created dir", tmp_wd)
|
|
29
|
-
|
|
30
|
-
const metadata = await get_metadata(hash)
|
|
31
|
-
|
|
32
|
-
const full_path = await download_file({tmp_wd, metadata})
|
|
33
|
-
|
|
34
|
-
if (img_preview) {
|
|
35
|
-
await apply_img_preview({tmp_wd, full_path, metadata})
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (ocr) {
|
|
39
|
-
if (!img_preview) {
|
|
40
|
-
throw new Error("img_preview cannot be false when ocr is set to true")
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
await run_ocr({tmp_wd, metadata})
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (text_vectors) {
|
|
47
|
-
if (!ocr) {
|
|
48
|
-
throw new Error("text_vectors requires ocr to be set to true")
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
await get_text_vectors({tmp_wd, metadata})
|
|
52
|
-
}
|
|
53
|
-
}
|