65 lines
2.0 KiB
TypeScript
Raw Normal View History

2023-01-05 02:28:36 +09:00
export const createDummyMediaStream = (audioContext: AudioContext) => {
const dummyOutputNode = audioContext.createMediaStreamDestination();
const gainNode = audioContext.createGain();
gainNode.gain.value = 0.0;
gainNode.connect(dummyOutputNode);
const oscillatorNode = audioContext.createOscillator();
oscillatorNode.frequency.value = 440;
oscillatorNode.connect(gainNode);
oscillatorNode.start();
return dummyOutputNode.stream;
};
2023-01-05 18:35:56 +09:00
export const fileSelector = async (regex: string) => {
const fileInput = document.createElement("input");
fileInput.type = "file";
const p = new Promise<File>((resolve, reject) => {
fileInput.onchange = (e) => {
if (e.target instanceof HTMLInputElement == false) {
2023-09-25 13:25:07 +09:00
console.log("invalid target!", e.target);
reject("invalid target");
return null;
2023-01-05 18:35:56 +09:00
}
2023-09-25 13:25:07 +09:00
const target = e.target as HTMLInputElement;
2023-01-05 18:35:56 +09:00
if (!target.files || target.files.length == 0) {
2023-09-25 13:25:07 +09:00
reject("no file selected");
return null;
2023-01-05 18:35:56 +09:00
}
if (regex != "" && target.files[0].type.match(regex)) {
reject(`not target file type ${target.files[0].type}`);
2023-09-25 13:25:07 +09:00
return null;
2023-01-05 18:35:56 +09:00
}
2023-09-25 13:25:07 +09:00
resolve(target.files[0]);
return null;
2023-01-05 18:35:56 +09:00
};
fileInput.click();
});
2023-09-25 13:25:07 +09:00
return await p;
};
2023-01-05 18:35:56 +09:00
export const fileSelectorAsDataURL = async (regex: string) => {
2023-09-25 13:25:07 +09:00
const f = await fileSelector(regex);
2023-01-05 18:35:56 +09:00
if (!f) {
2023-09-25 13:25:07 +09:00
return f;
2023-01-05 18:35:56 +09:00
}
const url = await new Promise<string>((resolve) => {
const reader = new FileReader();
reader.onload = () => {
console.log("load data", reader.result as string);
resolve(reader.result as string);
};
reader.readAsDataURL(f);
2023-09-25 13:25:07 +09:00
});
return url;
};
2023-01-07 20:07:39 +09:00
2023-01-08 09:22:22 +09:00
export const validateUrl = (url: string) => {
2023-02-21 17:54:02 +09:00
if (url?.endsWith("/")) {
2023-09-25 13:25:07 +09:00
return url.substring(0, url.length - 1);
2023-01-08 09:22:22 +09:00
}
2023-09-25 13:25:07 +09:00
return url;
};