Skip to content

Commit

Permalink
feat: add restrictions on file types
Browse files Browse the repository at this point in the history
  • Loading branch information
Satont committed Aug 23, 2023
1 parent f0dc9fd commit 3d814a9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
14 changes: 12 additions & 2 deletions apps/api/internal/impl_protected/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"bytes"
"context"
"fmt"
"log/slog"
"strings"

"github.com/google/uuid"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
Expand All @@ -14,7 +17,6 @@ import (
"github.com/twitchtv/twirp"
"google.golang.org/protobuf/types/known/emptypb"
"gorm.io/gorm"
"log/slog"
)

type Files struct {
Expand Down Expand Up @@ -92,6 +94,8 @@ func New(deps *impl_deps.Deps) *Files {
// 10MB
const bytesRestriction = (1 << 20) * 10

var acceptedMimeTypes = []string{"audio", "image"}

func (c *Files) FilesUpload(ctx context.Context, req *files.UploadRequest) (
*files.FileMeta,
error,
Expand All @@ -104,6 +108,12 @@ func (c *Files) FilesUpload(ctx context.Context, req *files.UploadRequest) (
return nil, twirp.NewError(twirp.OutOfRange, "File name is too long")
}

if !lo.SomeBy(acceptedMimeTypes, func(t string) bool {
return strings.HasPrefix(req.Mimetype, t)
}) {
return nil, twirp.NewError(twirp.OutOfRange, "Wrong file type")
}

dashboardId := ctx.Value("dashboardId").(string)

type NResult struct {
Expand All @@ -123,7 +133,7 @@ func (c *Files) FilesUpload(ctx context.Context, req *files.UploadRequest) (
}

if filesSize.N+len(req.Content) > bytesRestriction*10 {
return nil, twirp.NewError(twirp.OutOfRange, "Limit reached")
return nil, twirp.NewError(twirp.OutOfRange, "Limit of storage reached")
}

fileEntity := model.ChannelFile{
Expand Down
21 changes: 16 additions & 5 deletions frontend/dashboard/src/components/files/files.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script setup lang="ts">
import { RpcError } from '@protobuf-ts/runtime-rpc';
import { IconArchive } from '@tabler/icons-vue';
import { FileMeta } from '@twir/grpc/generated/api/api/files';
import {
Expand All @@ -13,6 +14,7 @@ import {
NText,
NUpload,
NUploadDragger,
useMessage,
} from 'naive-ui';
import { computed, onMounted, ref } from 'vue';
import { useI18n } from 'vue-i18n';
Expand All @@ -26,10 +28,6 @@ const { t } = useI18n();
const uploader = useFileUpload();
const deleter = userFileDelete();
async function upload(f: File) {
await uploader.mutateAsync(f);
}
const { data: files } = useFiles();
const uploadedFilesSize = computed(() => {
Expand Down Expand Up @@ -76,11 +74,24 @@ onMounted(() => {
const audios = computed(() => files.value?.files.filter(f => f.mimetype.startsWith('audio')) ?? []);
defineEmits<{
select: [id: string],
delete: [id: string]
}>();
const message = useMessage();
async function upload(f: File) {
if (!f.type.startsWith(activeTab.value.accept.split('*').at(0)!)) return;
try {
await uploader.mutateAsync(f);
} catch (error) {
if (error instanceof RpcError) {
message.error(error.message);
}
}
}
</script>

<template>
Expand Down

0 comments on commit 3d814a9

Please sign in to comment.