added file type check for import
This commit is contained in:
@@ -8,6 +8,8 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
@@ -179,6 +181,15 @@ func UploadFiles(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "file_vtt is required"})
|
||||
return
|
||||
}
|
||||
if !isValidFileType(file_vtt.Filename) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "File1 must be a .csv or .json file"})
|
||||
return
|
||||
}
|
||||
// Validate file2 if provided
|
||||
if file_csv != nil && !isValidFileType(file_csv.Filename) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "File2 must be a .csv or .json file"})
|
||||
return
|
||||
}
|
||||
|
||||
// Save File 1
|
||||
err := c.SaveUploadedFile(file_vtt, fmt.Sprintf("./uploads/%s", file_vtt.Filename))
|
||||
@@ -198,3 +209,14 @@ func UploadFiles(c *gin.Context) {
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Files uploaded successfully"})
|
||||
}
|
||||
|
||||
func isValidFileType(filename string) bool {
|
||||
allowedExtensions := []string{".csv", ".json"}
|
||||
ext := strings.ToLower(filepath.Ext(filename))
|
||||
for _, allowedExt := range allowedExtensions {
|
||||
if ext == allowedExt {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -29,11 +29,28 @@ export default {
|
||||
success: "",
|
||||
};
|
||||
},
|
||||
computed: {
|
||||
hasInvalidFileType() {
|
||||
return !this.isValidFileType(this.file_vtt) || (this.file_csv && !this.isValidFileType(this.file_csv));
|
||||
},
|
||||
},
|
||||
methods: {
|
||||
onFileChange(event, fileNumber) {
|
||||
const file = event.target.files[0];
|
||||
if (fileNumber === 1) this.file_vtt = file;
|
||||
if (fileNumber === 2) this.file_csv = file;
|
||||
|
||||
// Validate file type
|
||||
if (!this.isValidFileType(file)) {
|
||||
this.error = "Invalid file type. Only .csv and .json files are allowed.";
|
||||
return;
|
||||
}
|
||||
this.error = ""; // Clear any previous error
|
||||
},
|
||||
isValidFileType(file) {
|
||||
if (!file) return false;
|
||||
const allowedTypes = ["application/json", "text/csv"];
|
||||
return allowedTypes.includes(file.type);
|
||||
},
|
||||
async handleUpload() {
|
||||
try {
|
||||
@@ -41,9 +58,11 @@ export default {
|
||||
formData.append("file_vtt", this.file_vtt);
|
||||
if (this.file_csv) formData.append("file_csv", this.file_csv);
|
||||
|
||||
const token = localStorage.getItem("token"); // Get token from storage
|
||||
const response = await API.post("/upload", formData, {
|
||||
headers: {
|
||||
"Content-Type": "multipart/form-data",
|
||||
"Authorization": `Bearer ${token}`, // Include token in the header
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user