Skip to content

Commit

Permalink
Merge pull request #60 from rohan-b-84/main
Browse files Browse the repository at this point in the history
fix: set upload limit to 10
  • Loading branch information
harshkhandeparkar authored May 29, 2024
2 parents 12265c4 + c2892c3 commit e5ff722
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
3 changes: 2 additions & 1 deletion backend/.env.template
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ DB_PORT=
DB_USER=
DB_PASSWORD=
STATIC_FILES_STORAGE_LOCATION=/srv/static
UPLOADED_QPS_PATH=iqps/uploaded # Relative to `STATIC_FILES_STORAGE_LOCATION`. Final upload location will be /srv/static/iqps/uploaded
UPLOADED_QPS_PATH=iqps/uploaded # Relative to `STATIC_FILES_STORAGE_LOCATION`. Final upload location will be /srv/static/iqps/uploaded
MAX_UPLOAD_LIMIT=10
10 changes: 8 additions & 2 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,14 @@ func upload(w http.ResponseWriter, r *http.Request) {
return
}

maxLimit, err := strconv.Atoi(os.Getenv("MAX_UPLOAD_LIMIT"))
if err != nil || maxLimit < 1 {
maxLimit = 10
}

files := r.MultipartForm.File["files"]
if len(files) > 7 {
http.Error(w, "max 7 files allowed", http.StatusBadRequest)
if len(files) > maxLimit {
http.Error(w, fmt.Sprintf("maximum %d files allowed", maxLimit), http.StatusBadRequest)
return
}

Expand Down Expand Up @@ -318,6 +323,7 @@ func main() {
host := os.Getenv("DB_HOST")
port, err := strconv.Atoi(os.Getenv("DB_PORT"))
CheckError(err)

user := os.Getenv("DB_USER")
password := os.Getenv("DB_PASSWORD")
dbname := os.Getenv("DB_NAME")
Expand Down
1 change: 1 addition & 0 deletions frontend/.env.development
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
VITE_BACKEND_URL=http://localhost:5000
VITE_MAX_UPLOAD_LIMIT=10
1 change: 1 addition & 0 deletions frontend/.env.production
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
VITE_BACKEND_URL=https://iqps-server.metakgp.org
VITE_MAX_UPLOAD_LIMIT=10
19 changes: 17 additions & 2 deletions frontend/src/pages/UploadPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import { Spinner } from "../components/Spinner";
import { validate } from "../utils/validateInput";

const UploadPage: Component = () => {
let MAX_UPLOAD_LIMIT = parseInt(import.meta.env.VITE_MAX_UPLOAD_LIMIT)
if (isNaN(MAX_UPLOAD_LIMIT) || MAX_UPLOAD_LIMIT < 1) {
MAX_UPLOAD_LIMIT = 10
}
const [qPapers, setQPapers] = createSignal<IQuestionPaperFile[]>([]);
const [isDragging, setIsDragging] = createSignal(false);
const [selectedQPaper, setSelectedQPaper] =
Expand Down Expand Up @@ -74,6 +78,10 @@ const UploadPage: Component = () => {
(file) => file.type === "application/pdf"
);
if (pdfFiles && pdfFiles.length > 0) {
if (pdfFiles.length > MAX_UPLOAD_LIMIT) {
toast.error(`max ${MAX_UPLOAD_LIMIT} files allowed`);
return
}
addQPapers(pdfFiles);
} else {
toast.error("Could not catch files. Please try again");
Expand All @@ -92,9 +100,16 @@ const UploadPage: Component = () => {
e.stopPropagation();
setIsDragging(false);
};

const handleUpload = async (e: Event) => {
e.preventDefault();
if (qPapers().length > MAX_UPLOAD_LIMIT) {
toast.error(`max ${MAX_UPLOAD_LIMIT} files allowed`);
return;
}

const allValid = qPapers().every((qp) => isValid(qp));

if (!allValid) {
toast.error("Please provide correct course details");
return;
Expand Down Expand Up @@ -235,9 +250,9 @@ const UploadPage: Component = () => {
<><UploadIcon size="1.5rem" />Upload</>
)}
</button>
<button onClick={openFileDialog}>
{qPapers().length <= MAX_UPLOAD_LIMIT && <button onClick={openFileDialog}>
<FileAddIcon size="1.5rem" />Add More Files
</button>
</button>}
</div>
</>
) : (
Expand Down

0 comments on commit e5ff722

Please sign in to comment.