1 | /**
|
---|
2 | * The transcription object returned from our transcription endpoint.
|
---|
3 | *
|
---|
4 | * @typedef {Object} TranscriptionModel
|
---|
5 | * @property {String} file_name The name of the file that was transcribed.
|
---|
6 | * @property {String} log A note of how the transcription was processed.
|
---|
7 | * @property {{char: String, start_time: Number}[]} metadata The character metadata.
|
---|
8 | * @property {boolean} success A value indicating if the transcription was successful or not.
|
---|
9 | * @property {String} transcription The transcription.
|
---|
10 | */
|
---|
11 |
|
---|
12 | class TranscribeError
|
---|
13 | {
|
---|
14 | /**
|
---|
15 | * Initialises a new instance of the {@link TranscribeError} object.
|
---|
16 | *
|
---|
17 | * @param {Number | undefined} statusCode The status code of the error.
|
---|
18 | * @param {String | undefined} statusMessage The status message.
|
---|
19 | */
|
---|
20 | constructor(statusCode, statusMessage)
|
---|
21 | {
|
---|
22 | this.statusCode = statusCode;
|
---|
23 | this.statusMessage = statusMessage;
|
---|
24 | }
|
---|
25 | }
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * A service that uploads audio files in a multipart request to the Korero Maori API Interface servlet, and interprets the result.
|
---|
29 | */
|
---|
30 | class TranscribeService
|
---|
31 | {
|
---|
32 | constructor()
|
---|
33 | {
|
---|
34 | /** @type {String} The URL to which query POST requests should be made */
|
---|
35 | this.queryUrl = "/gs3-koreromaori/transcribe";
|
---|
36 | }
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Performs a query to transcribe the given audio files.
|
---|
40 | *
|
---|
41 | * @param {FileList} files The files to upload.
|
---|
42 | * @returns {Promise<TranscriptionModel[]>} The transcribed aufio file.
|
---|
43 | */
|
---|
44 | async transcribeFiles(files)
|
---|
45 | {
|
---|
46 | const that = this;
|
---|
47 | const formData = new FormData();
|
---|
48 |
|
---|
49 | let audioFileKeys = "";
|
---|
50 | for (let i = 0; i < files.length; i++)
|
---|
51 | {
|
---|
52 | const f = files[i];
|
---|
53 | const key = "audioFile" + i;
|
---|
54 |
|
---|
55 | formData.append(key, f, f.name);
|
---|
56 | audioFileKeys += key + "|";
|
---|
57 | }
|
---|
58 | formData.append("audioFileKeys", audioFileKeys);
|
---|
59 |
|
---|
60 | let response;
|
---|
61 | try
|
---|
62 | {
|
---|
63 | response = await fetch
|
---|
64 | (
|
---|
65 | that.queryUrl,
|
---|
66 | {
|
---|
67 | method: "POST",
|
---|
68 | body: formData
|
---|
69 | }
|
---|
70 | );
|
---|
71 | }
|
---|
72 | catch (e)
|
---|
73 | {
|
---|
74 | console.error(`Transcription failed with reason ${e}`);
|
---|
75 | throw new TranscribeError(undefined, undefined);
|
---|
76 | }
|
---|
77 |
|
---|
78 | if (!response.ok)
|
---|
79 | {
|
---|
80 | console.error(`Transcription API failed with status ${response.status} and message ${response.statusText}`);
|
---|
81 | throw new TranscribeError(response.status, response.statusText);
|
---|
82 | }
|
---|
83 |
|
---|
84 | return await response.json();
|
---|
85 | }
|
---|
86 |
|
---|
87 | // OBSOLETE. Kept for reference purposes. See doFetchUpload instead.
|
---|
88 | //
|
---|
89 | // Based on https://stackoverflow.com/questions/2320069/jquery-ajax-file-upload
|
---|
90 | // doUpload(files, resultCallback, progressCallback)
|
---|
91 | // {
|
---|
92 | // let that = this;
|
---|
93 | // let formData = new FormData();
|
---|
94 |
|
---|
95 | // let audioFileKeys = "";
|
---|
96 | // for (let f of files)
|
---|
97 | // {
|
---|
98 | // formData.append(f.name, f, f.name);
|
---|
99 | // audioFileKeys += f.name + "|";
|
---|
100 | // }
|
---|
101 | // formData.append("audioFileKeys", audioFileKeys);
|
---|
102 |
|
---|
103 | // $.ajax({
|
---|
104 | // type: "POST",
|
---|
105 | // url: this.queryUrl,
|
---|
106 | // xhr: function ()
|
---|
107 | // {
|
---|
108 | // var myXhr = $.ajaxSettings.xhr();
|
---|
109 | // if (myXhr.upload && progressCallback != undefined)
|
---|
110 | // {
|
---|
111 | // myXhr.upload.addEventListener("progress", progressCallback, false);
|
---|
112 | // }
|
---|
113 | // return myXhr;
|
---|
114 | // },
|
---|
115 | // success: function (data)
|
---|
116 | // {
|
---|
117 | // resultCallback(data);
|
---|
118 | // },
|
---|
119 | // error: function (error)
|
---|
120 | // {
|
---|
121 | // console.error("Failed to upload file via AJAX POST: " + error);
|
---|
122 | // },
|
---|
123 | // async: true,
|
---|
124 | // data: formData,
|
---|
125 | // cache: false,
|
---|
126 | // contentType: false,
|
---|
127 | // processData: false,
|
---|
128 | // timeout: 60000 // TODO: Consider if we need a longer timeout.
|
---|
129 | // });
|
---|
130 | // }
|
---|
131 | }
|
---|