Skip to content

Commit

Permalink
Accept more content-types (#542)
Browse files Browse the repository at this point in the history
* Accept more content-types

- Added new content-types to assist.ts
- Added new tests for new content-types

* Update tests/assist.handler.spec.ts

Changed expected content types to match test.

Co-authored-by: Joakim Sørensen <joasoe@gmail.com>

* Update tests/assist.handler.spec.ts

Changed expected content types to match test.

Co-authored-by: Joakim Sørensen <joasoe@gmail.com>

* Updated key extension

- Key extension will now represent what content type was received.

* Added new test for checking "content-type": "audio/ogg;codec=opus"

* Update ContentType to handle ";[data]"

---------

Co-authored-by: Joakim Sørensen <joasoe@gmail.com>
Co-authored-by: Pierre <397503+bemble@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 5, 2024
1 parent 42dbbd8 commit 3e04c1c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
13 changes: 9 additions & 4 deletions src/services/assist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { WorkerEvent } from "../common";
enum TRIGGER_PATH {
WAKE_WORD_TRAINING_UPLOAD = "/assist/wake_word/training_data/upload",
}
const WAKE_WORD_ALLOWED_CONTENT_TYPES = ["audio/webm"];
const WAKE_WORD_ALLOWED_CONTENT_TYPES = [
"audio/webm",
"audio/ogg",
"audio/mp4",
];
const WAKE_WORD_ALLOWED_NAMES = ["casita", "ok_nabu"];
const WAKE_WORD_MAX_CONTENT_LENGTH = 250 * 1024;
const USER_CONTENT_MAX_CONTENT_LENGTH = 150;
Expand All @@ -25,7 +29,7 @@ const createResponse = (options: {

const handleUploadAudioFile = async (event: WorkerEvent): Promise<Response> => {
const { request } = event;
const contentType = request.headers.get("content-type");
const contentType = request.headers.get("content-type").split(";")[0];
const contentLength = parseInt(request.headers.get("content-length"), 10);
const cfRay = request.headers.get("cf-ray");

Expand Down Expand Up @@ -80,8 +84,9 @@ const handleUploadAudioFile = async (event: WorkerEvent): Promise<Response> => {
},
});
}

const key = `${wakeWord}-${sanitizedUserContent}-${cfRay}.webm`;

const keyExtension = contentType.replace("audio/", "");
const key = `${wakeWord}-${sanitizedUserContent}-${cfRay}.${keyExtension}`;

await event.env.WAKEWORD_TRAINING_BUCKET.put(key, request.body);

Expand Down
7 changes: 6 additions & 1 deletion tests/assist.handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import { WorkerEvent } from "../src/common";
const USER_CONTENT_TO_MANY_CHARACTERS =
"rrdgY445SJ6TlXFDFUpWJFy29hudyZQsL8cYRYzlAutBJdoweJRPVphWMr6qprory8sYfe6WXSDn5hv293CAP8ybzBM22Ju3LIKKBwrWookqptAZmpydYokTovItHHIWHq7vnmzLYBB1jTDioFcFUeR";
const USER_CONTENT_VALID = "hello world";
const FILES_VALID = [{ contentType: "audio/webm", fileExtension: ".webm" }];
const FILES_VALID = [
{ contentType: "audio/webm", fileExtension: ".webm" },
{ contentType: "audio/ogg", fileExtension: ".ogg" },
{ contentType: "audio/mp4", fileExtension: ".mp4" },
{ contentType: "audio/ogg;codec=opus", fileExtension: ".ogg" }
];

describe("Assist handler", function () {
let MockRequest: any;
Expand Down

0 comments on commit 3e04c1c

Please sign in to comment.