streamby-core 0.4.0 → 0.5.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/dist/adapters/s3.js +13 -0
- package/dist/middleware/createRouter.js +27 -0
- package/dist/types/index.d.ts +7 -0
- package/package.json +2 -1
package/dist/adapters/s3.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createS3Adapter = createS3Adapter;
|
|
4
|
+
const s3_request_presigner_1 = require("@aws-sdk/s3-request-presigner");
|
|
4
5
|
const client_s3_1 = require("@aws-sdk/client-s3");
|
|
5
6
|
function createS3Adapter(config) {
|
|
6
7
|
const s3 = new client_s3_1.S3Client({
|
|
@@ -11,6 +12,18 @@ function createS3Adapter(config) {
|
|
|
11
12
|
},
|
|
12
13
|
});
|
|
13
14
|
return {
|
|
15
|
+
async getPresignedUrl(projectId, filename, type) {
|
|
16
|
+
const key = type === 'project-image'
|
|
17
|
+
? `projects/${projectId}/image-${Date.now()}-${filename}`
|
|
18
|
+
: `${projectId}/${filename}`;
|
|
19
|
+
const command = new client_s3_1.PutObjectCommand({
|
|
20
|
+
Bucket: config.bucket,
|
|
21
|
+
Key: key,
|
|
22
|
+
ContentType: 'image/png' // o dinámico
|
|
23
|
+
});
|
|
24
|
+
const url = await (0, s3_request_presigner_1.getSignedUrl)(s3, command, { expiresIn: 60 });
|
|
25
|
+
return { url, key };
|
|
26
|
+
},
|
|
14
27
|
async uploadFile(req, projectId) {
|
|
15
28
|
const file = req.body?.file;
|
|
16
29
|
if (!file || !file.name || !file.buffer) {
|
|
@@ -18,6 +18,33 @@ function createStreamByRouter(config) {
|
|
|
18
18
|
throw new Error('Unsupported storage type');
|
|
19
19
|
}
|
|
20
20
|
})();
|
|
21
|
+
router.get('/auth', async (req, res) => {
|
|
22
|
+
try {
|
|
23
|
+
const auth = await config.authProvider(req);
|
|
24
|
+
res.status(200).json({ logged: true, ...auth });
|
|
25
|
+
}
|
|
26
|
+
catch (err) {
|
|
27
|
+
res.status(401).json({ logged: false });
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
router.post('/upload-image', async (req, res) => {
|
|
31
|
+
try {
|
|
32
|
+
const auth = await config.authProvider(req);
|
|
33
|
+
if (auth.role !== 'admin' && auth.role !== 'editor') {
|
|
34
|
+
return res.status(403).json({ error: 'Permission denied' });
|
|
35
|
+
}
|
|
36
|
+
const { filename, type } = req.body;
|
|
37
|
+
const projectId = (req.query.projectId || req.headers['x-project-id']);
|
|
38
|
+
if (!filename || !projectId) {
|
|
39
|
+
return res.status(400).json({ error: 'Missing filename or projectId' });
|
|
40
|
+
}
|
|
41
|
+
const presigned = await adapter.getPresignedUrl(projectId, filename, type);
|
|
42
|
+
res.status(200).json(presigned);
|
|
43
|
+
}
|
|
44
|
+
catch (err) {
|
|
45
|
+
res.status(500).json({ error: 'Failed to generate presigned URL', details: err.message });
|
|
46
|
+
}
|
|
47
|
+
});
|
|
21
48
|
router.get('/files', async (req, res) => {
|
|
22
49
|
try {
|
|
23
50
|
const auth = await config.authProvider(req);
|
package/dist/types/index.d.ts
CHANGED
|
@@ -7,12 +7,17 @@ export interface S3Config {
|
|
|
7
7
|
}
|
|
8
8
|
export type StorageProviderType = 's3';
|
|
9
9
|
export interface StorageAdapter {
|
|
10
|
+
getPresignedUrl(projectId: string, filename: string, type?: string): Promise<{
|
|
11
|
+
url: string;
|
|
12
|
+
key: string;
|
|
13
|
+
}>;
|
|
10
14
|
uploadFile(req: Request, projectId: string): Promise<any>;
|
|
11
15
|
listFiles(projectId: string): Promise<any[]>;
|
|
12
16
|
}
|
|
13
17
|
export interface ProjectInfo {
|
|
14
18
|
id: string;
|
|
15
19
|
name: string;
|
|
20
|
+
image?: string;
|
|
16
21
|
description?: string;
|
|
17
22
|
rootFolders?: FolderNode[];
|
|
18
23
|
settings?: {
|
|
@@ -35,6 +40,7 @@ export interface StreamByConfig {
|
|
|
35
40
|
}
|
|
36
41
|
export interface AuthContext {
|
|
37
42
|
userId: string;
|
|
43
|
+
username: string;
|
|
38
44
|
projects: string[];
|
|
39
45
|
role: 'viewer' | 'editor' | 'admin';
|
|
40
46
|
}
|
|
@@ -43,6 +49,7 @@ export interface ProjectProvider {
|
|
|
43
49
|
getById(projectId: string): Promise<ProjectInfo>;
|
|
44
50
|
create(data: {
|
|
45
51
|
name: string;
|
|
52
|
+
image?: string;
|
|
46
53
|
description?: string;
|
|
47
54
|
allowUpload?: boolean;
|
|
48
55
|
allowSharing?: boolean;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "streamby-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "StreamBy middleware framework for media storage management",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
"license": "MIT",
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"@aws-sdk/client-s3": "^3.528.0",
|
|
33
|
+
"@aws-sdk/s3-request-presigner": "^3.787.0",
|
|
33
34
|
"express": "^4.0.0"
|
|
34
35
|
},
|
|
35
36
|
"devDependencies": {
|