powr-sdk-api 4.10.0 → 4.10.2

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.
@@ -12,7 +12,7 @@ const {
12
12
 
13
13
  const router = express.Router();
14
14
  const upload = multer();
15
- router.post('/', upload.single('file'), async (req, res) => {
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();
@@ -10,37 +10,41 @@ const {
10
10
  config,
11
11
  credentials
12
12
  } = require('../config');
13
- const storage = new Storage({
14
- projectId: config.projectId,
15
- credentials: credentials
13
+ const gcpProject = process.env.GOOGLE_CLOUD_PROJECT_ID || process.env.GOOGLE_CLOUD_UPLOAD_PROJECT_ID || process.env.GCS_PROJECT_ID || process.env.GOOGLE_CLOUD_PROJECT;
14
+ const onCloudRun = Boolean(process.env.K_SERVICE);
15
+ const hasKeyFile = Boolean(credentials.client_email && credentials.private_key && credentials.private_key.includes('BEGIN PRIVATE KEY'));
16
+
17
+ // Cloud Run: use the runtime service account. JSON keys in .env often point
18
+ // at deleted SAs (invalid_grant: account not found).
19
+ const storage = new Storage(!onCloudRun && hasKeyFile ? {
20
+ projectId: gcpProject || credentials.project_id,
21
+ credentials
22
+ } : {
23
+ projectId: gcpProject
16
24
  });
17
25
  const bucketName = config.storageBucket;
18
26
  const bucket = storage.bucket(bucketName);
19
- console.log('Bucket:', bucketName);
20
- console.log('Bucket:', bucket);
21
27
  async function uploadToGCS(file) {
28
+ if (!bucketName) {
29
+ throw new Error('STORAGE_BUCKET is not set');
30
+ }
22
31
  const timestamp = format(new Date(), 'yyyyMMdd-HHmmss');
23
32
  const cleanFileName = file.originalname.replace(/[^a-zA-Z0-9.-]/g, '-');
24
33
  const filename = `${timestamp}-${cleanFileName}`;
25
34
  const blob = bucket.file(filename);
26
35
  try {
27
- console.log('Preparing to upload file:', {
28
- name: file.originalname,
29
- type: file.mimetype
30
- });
31
- const buffer = file.buffer;
32
- console.log('Uploading file to GCS...');
33
- await blob.save(buffer, {
36
+ await blob.save(file.buffer, {
34
37
  contentType: file.mimetype,
35
38
  metadata: {
36
39
  contentType: file.mimetype
37
40
  }
38
41
  });
39
- await blob.makePublic();
40
- console.log('File made public successfully');
41
- const publicUrl = `https://storage.googleapis.com/${bucketName}/${filename}`;
42
- console.log('File uploaded and public URL generated:', publicUrl);
43
- return publicUrl;
42
+ try {
43
+ await blob.makePublic();
44
+ } catch (aclError) {
45
+ console.warn('GCS makePublic skipped:', aclError.message);
46
+ }
47
+ return `https://storage.googleapis.com/${bucketName}/${filename}`;
44
48
  } catch (error) {
45
49
  console.error('Error uploading file to GCS:', error);
46
50
  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.0",
3
+ "version": "4.10.2",
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",