partey_workadventure/front/src/Components/ConsoleGlobalMessageManager/UploadAudioGlobalMessage.svelte

98 lines
3.4 KiB
Svelte
Raw Normal View History

<script lang="ts">
import {HtmlUtils} from "../../WebRtc/HtmlUtils";
2021-06-22 09:43:41 +02:00
import type {Game} from "../../Phaser/Game/Game";
2021-06-22 10:10:03 +02:00
import type {GameManager} from "../../Phaser/Game/GameManager";
2021-06-22 16:12:53 +02:00
import {consoleGlobalMessageManagerFocusStore, consoleGlobalMessageManagerVisibleStore} from "../../Stores/ConsoleGlobalMessageManagerStore";
import {AdminMessageEventTypes} from "../../Connexion/AdminMessagesService";
import type {PlayGlobalMessageInterface} from "../../Connexion/ConnexionModels";
import uploadFile from "../images/music-file.svg";
interface EventTargetFiles extends EventTarget {
files: Array<File>;
}
export let game: Game;
export let gameManager: GameManager;
let gameScene = gameManager.getCurrentGameScene(game.findAnyScene());
2021-06-22 10:10:03 +02:00
let fileinput: HTMLInputElement;
let filename: string;
let filesize: string;
2021-06-22 09:43:41 +02:00
let errorfile: boolean;
const AUDIO_TYPE = AdminMessageEventTypes.audio;
async function SendAudioMessage() {
2021-06-22 10:27:54 +02:00
if (gameScene == undefined) {
return;
}
2021-06-22 09:43:41 +02:00
const inputAudio = HtmlUtils.getElementByIdOrFail<HTMLInputElement>("input-send-audio");
2021-06-22 10:22:49 +02:00
const selectedFile = inputAudio.files ? inputAudio.files[0] : null;
if (!selectedFile) {
errorfile = true;
throw 'no file selected';
}
2021-06-22 10:22:49 +02:00
const fd = new FormData();
fd.append('file', selectedFile);
2021-06-22 10:43:20 +02:00
const res = await gameScene.connection?.uploadAudio(fd);
2021-06-22 10:22:49 +02:00
const GlobalMessage: PlayGlobalMessageInterface = {
id: (res as { id: string }).id,
message: (res as { path: string }).path,
type: AUDIO_TYPE
}
2021-06-22 10:22:49 +02:00
inputAudio.value = '';
2021-06-22 10:43:20 +02:00
gameScene.connection?.emitGlobalMessage(GlobalMessage);
2021-06-22 10:22:49 +02:00
disableConsole();
}
function inputAudioFile(event: Event) {
const eventTarget : EventTargetFiles = (event.target as EventTargetFiles);
if(!eventTarget || !eventTarget.files || eventTarget.files.length === 0){
return;
}
const file = eventTarget.files[0];
if(!file) {
return;
}
filename = file.name;
filesize = getFileSize(file.size);
2021-06-22 09:43:41 +02:00
errorfile = false;
}
function getFileSize(number: number) {
if (number < 1024) {
return number + 'bytes';
} else if (number >= 1024 && number < 1048576) {
return (number / 1024).toFixed(1) + 'KB';
} else if (number >= 1048576) {
return (number / 1048576).toFixed(1) + 'MB';
} else {
return '';
}
}
function disableConsole() {
2021-06-22 16:12:53 +02:00
consoleGlobalMessageManagerVisibleStore.set(false);
consoleGlobalMessageManagerFocusStore.set(false);
}
</script>
<section class="section-input-send-audio">
<img src="{uploadFile}" alt="Upload a file" on:click|preventDefault={ () => {fileinput.click();}}>
{#if filename !== undefined}
<label for="input-send-audio">{filename} : {filesize}</label>
{/if}
2021-06-22 09:43:41 +02:00
{#if errorfile}
<p class="err">No file selected. You need to upload a file before sending it.</p>
{/if}
<input type="file" id="input-send-audio" bind:this={fileinput} on:change={(e) => {inputAudioFile(e)}}>
<div class="footer-btn-action">
<button class="nes-btn is-primary" on:click|preventDefault={SendAudioMessage}>Send</button>
</div>
</section>