Skip to content

Commit

Permalink
Merge pull request #6 from soracom-labs/harvest-files-path-handling-i…
Browse files Browse the repository at this point in the history
…mprovement

Make soracom-image-source that can handle upload path upon webhook request
  • Loading branch information
yuta-imai authored Jul 22, 2024
2 parents 261c277 + ad5ff85 commit 995e7dc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ A source webhook which uploads SorCam image to Soracom Harvest Files.
curl \
-XGET \
-H "x-api-key:${apikey}" \
"https://${hostname}/v1/source/soracam_image?device_id=${device_id}"
"https://${hostname}/v1/source/soracam_image?device_id=${device_id}&upload_directory=${directory}"
```

Query parameters
* device_id: Soracam ID
* upload_directory: Upload target directory under base path.

You will need SAM user with permissions below

Expand Down
11 changes: 8 additions & 3 deletions lambda/soracam-image-source.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,17 @@ describe("Lambda handler", () => {

expect(result.statusCode).toBe(400);
expect(JSON.parse(result.body).message).toBe(
"device is required in querystring parameters"
"device_id and upload_directory are required in querystring parameters"
);
});

it("should successfully process the request", async () => {
const event = { queryStringParameters: { device_id: "testDevice" } };
const event = {
queryStringParameters: {
device_id: "testDevice",
upload_directory: "testDirectory",
},
};

// Mock the Secrets Manager response
secretsManagerMock.on(GetSecretValueCommand).resolves({
Expand Down Expand Up @@ -87,7 +92,7 @@ describe("Lambda handler", () => {

soracomHttpClientMock
.onPut(
/https:\/\/api.soracom.io\/v1\/files\/private\/test\/path\/testDevice-\d{14}.jpg/
/https:\/\/api.soracom.io\/v1\/files\/private\/test\/path\/testDirectory\/testDevice-\d{14}.jpg/
)
.reply(201);

Expand Down
13 changes: 9 additions & 4 deletions lambda/soracam-image-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@ import { getSoracomClient, setGetSoracomClient } from "./lib/utils";
export { setGetSoracomClient };

export const handler = async (event: any = {}): Promise<any> => {
if (!event.queryStringParameters || !event.queryStringParameters.device_id) {
if (
!event.queryStringParameters ||
!event.queryStringParameters.device_id ||
!event.queryStringParameters.upload_directory
) {
return {
statusCode: 400,
body: JSON.stringify({
message: "device is required in querystring parameters",
message:
"device_id and upload_directory are required in querystring parameters",
}),
};
}

const harvestfilesPath = process.env.HARVEST_FILES_PATH!;

const deviceId = event.queryStringParameters.device_id;
const uploadDirectory = event.queryStringParameters.upload_directory;
const soracomClient = await getSoracomClient();

const time = Date.now() - 1000 * 10;
Expand All @@ -42,7 +47,7 @@ export const handler = async (event: any = {}): Promise<any> => {
}
await new Promise((resolve) => setTimeout(resolve, 1000));
}
let path = `${harvestfilesPath}/${deviceId}-${dayjs().format(
let path = `${harvestfilesPath}/${uploadDirectory}/${deviceId}-${dayjs().format(
"YYYYMMDDHHmmss"
)}.jpg`;

Expand Down

0 comments on commit 995e7dc

Please sign in to comment.