powr-sdk-api 4.10.0 → 4.10.1
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/routes/files.js +8 -3
- package/dist/services/uploadToGCS.js +12 -12
- package/package.json +1 -1
package/dist/routes/files.js
CHANGED
|
@@ -12,7 +12,7 @@ const {
|
|
|
12
12
|
|
|
13
13
|
const router = express.Router();
|
|
14
14
|
const upload = multer();
|
|
15
|
-
|
|
15
|
+
async function handleUpload(req, res) {
|
|
16
16
|
try {
|
|
17
17
|
if (!req.file) {
|
|
18
18
|
return res.status(400).json({
|
|
@@ -24,7 +24,10 @@ router.post('/', upload.single('file'), async (req, res) => {
|
|
|
24
24
|
return res.status(200).json({
|
|
25
25
|
status: 'success',
|
|
26
26
|
message: 'File uploaded successfully',
|
|
27
|
-
url
|
|
27
|
+
url,
|
|
28
|
+
originalname: req.file.originalname,
|
|
29
|
+
mimetype: req.file.mimetype,
|
|
30
|
+
size: req.file.size
|
|
28
31
|
});
|
|
29
32
|
} catch (err) {
|
|
30
33
|
console.error('Error uploading file:', err);
|
|
@@ -34,7 +37,9 @@ router.post('/', upload.single('file'), async (req, res) => {
|
|
|
34
37
|
error: err.message || 'Unknown error occurred'
|
|
35
38
|
});
|
|
36
39
|
}
|
|
37
|
-
}
|
|
40
|
+
}
|
|
41
|
+
router.post('/', upload.single('file'), handleUpload);
|
|
42
|
+
router.post('/upload', upload.single('file'), handleUpload);
|
|
38
43
|
router.get('/', async (req, res) => {
|
|
39
44
|
try {
|
|
40
45
|
const [files] = await bucket.getFiles();
|
|
@@ -16,31 +16,31 @@ const storage = new Storage({
|
|
|
16
16
|
});
|
|
17
17
|
const bucketName = config.storageBucket;
|
|
18
18
|
const bucket = storage.bucket(bucketName);
|
|
19
|
-
console.log('Bucket:', bucketName);
|
|
20
|
-
console.log('Bucket:', bucket);
|
|
21
19
|
async function uploadToGCS(file) {
|
|
20
|
+
if (!bucketName) {
|
|
21
|
+
throw new Error('STORAGE_BUCKET is not set');
|
|
22
|
+
}
|
|
22
23
|
const timestamp = format(new Date(), 'yyyyMMdd-HHmmss');
|
|
23
24
|
const cleanFileName = file.originalname.replace(/[^a-zA-Z0-9.-]/g, '-');
|
|
24
25
|
const filename = `${timestamp}-${cleanFileName}`;
|
|
25
26
|
const blob = bucket.file(filename);
|
|
26
27
|
try {
|
|
27
|
-
console.log('Preparing to upload file:', {
|
|
28
|
-
name: file.originalname,
|
|
29
|
-
type: file.mimetype
|
|
30
|
-
});
|
|
31
28
|
const buffer = file.buffer;
|
|
32
|
-
console.log('Uploading file to GCS...');
|
|
33
29
|
await blob.save(buffer, {
|
|
34
30
|
contentType: file.mimetype,
|
|
35
31
|
metadata: {
|
|
36
32
|
contentType: file.mimetype
|
|
37
33
|
}
|
|
38
34
|
});
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
35
|
+
|
|
36
|
+
// Uniform bucket-level access rejects object ACLs. Public URL still works
|
|
37
|
+
// if the bucket (or object) is already publicly readable.
|
|
38
|
+
try {
|
|
39
|
+
await blob.makePublic();
|
|
40
|
+
} catch (aclError) {
|
|
41
|
+
console.warn('GCS makePublic skipped:', aclError.message);
|
|
42
|
+
}
|
|
43
|
+
return `https://storage.googleapis.com/${bucketName}/${filename}`;
|
|
44
44
|
} catch (error) {
|
|
45
45
|
console.error('Error uploading file to GCS:', error);
|
|
46
46
|
throw new Error(`Failed to upload file to GCS: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "powr-sdk-api",
|
|
3
|
-
"version": "4.10.
|
|
3
|
+
"version": "4.10.1",
|
|
4
4
|
"description": "Shared API core library for PowrStack projects. Zero dependencies - works with Express, Next.js API routes, and other frameworks. All features are optional and install only what you need.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|