Skip to content

Commit

Permalink
feat: add file validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes-Andersen committed Dec 23, 2023
1 parent 6571050 commit 93cdf03
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion backend/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def transcribe() -> Any:
}
else:
tempFile = tempfile.NamedTemporaryFile(
dir='./backend/upload-shared-tmp', delete=False)
dir='./upload-shared-tmp', delete=False)

# Get the file from the request body and save it to a temporary file
file = request.data
Expand Down
32 changes: 26 additions & 6 deletions frontend/src/components/Upload.astro
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@ const { accentColor } = Astro.props;
style={`background-color: ${accentColor}`}
class="file-upload-button"
>
<input
id="file-upload"
name="file-dropzone-upload"
class="sr-only"
type="file"
/>
<input required id="file-upload" name="file-upload" class="sr-only" type="file" />
<svg xmlns="http://www.w3.org/2000/svg" width="25" height="24" fill="none">
<path
stroke="#fff"
Expand All @@ -48,6 +43,31 @@ const { accentColor } = Astro.props;
</label>
</form>

<script>
const allowedFileTypes = ["video/", "audio/", "application/ogg"];
const fileInput = document.getElementById("file-upload") as HTMLInputElement;

fileInput.addEventListener("change", (event: any) => {
const file = event.target?.files[0];

if (!file) {
fileInput.setCustomValidity("Please upload a file.");
fileInput.reportValidity();
return;
}

if (!allowedFileTypes.some((type) => file.type.includes(type))) {
fileInput.setCustomValidity(`Please upload a audio/video file. The file you uploaded is of type ${file.type}.`);
fileInput.reportValidity();
return;
}

fileInput.setCustomValidity("");
// TODO: Start the upload process
console.log("File accepted:", file.name);
});
</script>

<style>
.upload-form {
align-self: flex-start;
Expand Down

0 comments on commit 93cdf03

Please sign in to comment.