-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload-records.ts
105 lines (97 loc) · 3.59 KB
/
upload-records.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { DeepClient } from "@deep-foundation/deeplinks/imports/client.js";
import { PACKAGE_NAME } from './package-name.js';
import { ISound } from "./stop-recording.js";
import { LinkName } from "./link-name.js";
export interface IRecord { // Represents a record containing sound and its details.
sound: ISound; // The recorded sound.
startTime: number; // The start time of the recording.
endTime: number; // The end time of the recording.
}
export interface IUploadRecordsOptions { // Represents the parameters for uploading records.
deep: DeepClient; // The DeepClient instance.
containerLinkId: number; // The ID of the container link.
records: IRecord[]; // An array of records to be uploaded.
}
// uploadRecords function uploads the recorded sound with its details.
export async function uploadRecords({deep, containerLinkId, records}:IUploadRecordsOptions) {
// Get the link IDs for nessesary types.
const containTypeLinkId = await deep.id("@deep-foundation/core", "Contain");
const recordTypeLinkId = await deep.id(PACKAGE_NAME, LinkName[LinkName.Record]);
const durationTypeLinkId = await deep.id(PACKAGE_NAME, LinkName[LinkName.Duration]);
const startTimeTypeLinkId = await deep.id(PACKAGE_NAME, LinkName[LinkName.StartTime]);
const endTimeTypeLinkId = await deep.id(PACKAGE_NAME, LinkName[LinkName.EndTime]);
const mimetypeTypeLinkId = await deep.id("@deep-foundation/sound", LinkName[LinkName["MIME/type"]]);
const formatTypeLinkId = await deep.id("@deep-foundation/sound", LinkName[LinkName.Format]);
const soundTypeLinkId = await deep.id("@deep-foundation/sound", LinkName[LinkName.Sound]);
// Map the records to the links structure defined in package type links structure and insert into the database.
await deep.insert(records.map((record) => ({
type_id: recordTypeLinkId,
in: {
data: [{
type_id: containTypeLinkId,
from_id: containerLinkId,
}]
},
out: {
data: [
{
type_id: containTypeLinkId,
to: {
data: {
type_id: soundTypeLinkId,
string: { data: { value: record.sound["recordDataBase64"] } },
out: {
data: [
{
type_id: containTypeLinkId,
to: {
data: {
type_id: mimetypeTypeLinkId,
string: { data: { value: record.sound["mimeType"] } },
}
}
},
{
type_id: containTypeLinkId,
to: {
data: {
type_id: formatTypeLinkId,
string: { data: { value: record.sound["mimeType"] === "audio/webm;codecs=opus" ? "webm" : "aac" } },
}
}
}
]
}
}
}
},
{
type_id: containTypeLinkId,
to: {
data: {
type_id: durationTypeLinkId,
number: { data: { value: record.sound["msDuration"] } },
}
}
},
{
type_id: containTypeLinkId,
to: {
data: {
type_id: startTimeTypeLinkId,
number: { data: { value: record.startTime } },
}
}
},
{
type_id: containTypeLinkId,
to: {
data: {
type_id: endTimeTypeLinkId,
number: { data: { value: record.endTime } },
}
}
}]
}
})));
}