这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions lib/actions/google/gcs/google_cloud_storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const http_errors_1 = require("../../../error_types/http_errors");
const utils_1 = require("../../../error_types/utils");
const Hub = require("../../../hub");
const action_response_1 = require("../../../hub/action_response");
const storage = require("@google-cloud/storage");
const { Storage } = require("@google-cloud/storage");
const FILE_EXTENSION = new RegExp(/(.*)\.(.*)$/);
const LOG_PREFIX = "[Google Cloud Storage]";
class GoogleCloudStorageAction extends Hub.Action {
Expand Down Expand Up @@ -42,6 +42,7 @@ class GoogleCloudStorageAction extends Hub.Action {
];
}
async execute(request) {
var _a, _b;
const response = new Hub.ActionResponse();
if (!request.formParams.bucket) {
const error = (0, action_response_1.errorWith)(http_errors_1.HTTP_ERROR.bad_request, `${LOG_PREFIX} needs a GCS bucket specified.`);
Expand Down Expand Up @@ -75,12 +76,20 @@ class GoogleCloudStorageAction extends Hub.Action {
const gcs = this.gcsClientFromRequest(request);
const file = gcs.bucket(request.formParams.bucket)
.file(filename);
const writeStream = file.createWriteStream();
const writeStream = file.createWriteStream({
metadata: {
contentType: (_b = (_a = request.attachment) === null || _a === void 0 ? void 0 : _a.mime) !== null && _b !== void 0 ? _b : "application/octet-stream",
},
});
try {
await request.stream(async (readable) => {
return new Promise((resolve, reject) => {
readable.pipe(writeStream)
.on("error", reject)
.on("error", (error) => {
winston.error(`${LOG_PREFIX} Stream error: ${error.message}`, { error, webhookId: request.webhookId });
writeStream.end(); // Ensure stream is closed after an error
reject(error);
})
.on("finish", resolve);
});
});
Expand All @@ -93,7 +102,10 @@ class GoogleCloudStorageAction extends Hub.Action {
response.error = error;
response.message = error.message;
response.webhookId = request.webhookId;
winston.error(`${LOG_PREFIX} ${error.message}`, { error, webhookId: request.webhookId });
winston.error(`${LOG_PREFIX} Error uploading file. Error: ${error.message}`, {
error,
webhookId: request.webhookId,
});
return response;
}
}
Expand Down Expand Up @@ -147,11 +159,12 @@ class GoogleCloudStorageAction extends Hub.Action {
client_email: request.params.client_email,
private_key: request.params.private_key.replace(/\\n/g, "\n"),
};
const config = {
return new Storage({
projectId: request.params.project_id,
credentials,
};
return new storage(config);
apiEndpoint: "https://storage.googleapis.com",
useAuthWithCustomEndpoint: true,
});
}
}
exports.GoogleCloudStorageAction = GoogleCloudStorageAction;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"license": "MIT",
"dependencies": {
"@azure/storage-blob": "^12.26.0",
"@google-cloud/storage": "^1.5.2",
"@google-cloud/storage": "^7.16.0",
"@hubspot/api-client": "^2.0.1",
"@sendgrid/helpers": "7.2.0",
"@sendgrid/mail": "7.2.2",
Expand Down
31 changes: 21 additions & 10 deletions src/actions/google/gcs/google_cloud_storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { HTTP_ERROR } from "../../../error_types/http_errors"
import { getHttpErrorType } from "../../../error_types/utils"
import * as Hub from "../../../hub"
import { Error, errorWith } from "../../../hub/action_response"

const storage = require("@google-cloud/storage")
const { Storage } = require("@google-cloud/storage")

const FILE_EXTENSION = new RegExp(/(.*)\.(.*)$/)
const LOG_PREFIX = "[Google Cloud Storage]"
Expand Down Expand Up @@ -86,13 +85,21 @@ export class GoogleCloudStorageAction extends Hub.Action {
const gcs = this.gcsClientFromRequest(request)
const file = gcs.bucket(request.formParams.bucket)
.file(filename)
const writeStream = file.createWriteStream()
const writeStream = file.createWriteStream({
metadata: {
contentType: request.attachment?.mime ?? "application/octet-stream",
},
})

try {
await request.stream(async (readable) => {
return new Promise<any>((resolve, reject) => {
readable.pipe(writeStream)
.on("error", reject)
.on("error", (error: any) => {
winston.error(`${LOG_PREFIX} Stream error: ${error.message}`, {error, webhookId: request.webhookId})
writeStream.end() // Ensure stream is closed after an error
reject(error)
})
.on("finish", resolve)
})
})
Expand All @@ -110,15 +117,18 @@ export class GoogleCloudStorageAction extends Hub.Action {
response.message = error.message
response.webhookId = request.webhookId

winston.error(`${LOG_PREFIX} ${error.message}`, {error, webhookId: request.webhookId})
winston.error(
`${LOG_PREFIX} Error uploading file. Error: ${error.message}`, {
error,
webhookId: request.webhookId,
})
return response
}

}

async form(request: Hub.ActionRequest) {
const form = new Hub.ActionForm()

const gcs = this.gcsClientFromRequest(request)
let results: any

Expand Down Expand Up @@ -175,12 +185,13 @@ export class GoogleCloudStorageAction extends Hub.Action {
client_email: request.params.client_email,
private_key: request.params.private_key!.replace(/\\n/g, "\n"),
}
const config = {

return new Storage({
projectId: request.params.project_id,
credentials,
}

return new storage(config)
apiEndpoint: "https://storage.googleapis.com",
useAuthWithCustomEndpoint : true,
})
}

}
Expand Down
Loading