Skip to content

Commit

Permalink
imageupload: add the files required (#132)
Browse files Browse the repository at this point in the history
* imageupload: add the files required

* EventForm: change the no members message

* Fix the uploadImageFile function and calls

* Added back warnsizes in uploadImageFile

---------

Co-authored-by: Bhav Beri <[email protected]>
  • Loading branch information
abhiramtilakiiit and bhavberi authored Dec 22, 2024
1 parent 8a9a56b commit 55b7211
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 38 deletions.
16 changes: 10 additions & 6 deletions src/components/clubs/ClubForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ import { socialsData } from "utils/socialsData";
import { createClubAction } from "actions/clubs/create/server_action";
import { editClubAction } from "actions/clubs/edit/server_action";

const logo_warnSizeMB = 0.5;
const banner_warnSizeMB = 1.5;
const bannerSquare_warnSizeMB = 1;
const logo_maxSizeMB = 3;
const banner_maxSizeMB = 10;
const bannerSquare_maxSizeMB = 3;

const logo_warnSizeMB = 0.3;
const banner_warnSizeMB = 1;
const bannerSquare_warnSizeMB = 0.3;

export default function ClubForm({ defaultValues = {}, action = "log" }) {
const router = useRouter();
Expand Down Expand Up @@ -313,7 +317,7 @@ export default function ClubForm({ defaultValues = {}, action = "log" }) {
control={control}
maxFiles={1}
shape="circle"
maxSizeMB={5}
maxSizeMB={logo_maxSizeMB}
warnSizeMB={logo_warnSizeMB}
/>
</Grid>
Expand All @@ -325,7 +329,7 @@ export default function ClubForm({ defaultValues = {}, action = "log" }) {
control={control}
maxFiles={1}
shape="rectangle"
maxSizeMB={15}
maxSizeMB={banner_maxSizeMB}
warnSizeMB={banner_warnSizeMB}
/>
</Grid>
Expand All @@ -337,7 +341,7 @@ export default function ClubForm({ defaultValues = {}, action = "log" }) {
control={control}
maxFiles={1}
shape="square"
maxSizeMB={10}
maxSizeMB={bannerSquare_maxSizeMB}
warnSizeMB={bannerSquare_warnSizeMB}
/>
</Grid>
Expand Down
22 changes: 8 additions & 14 deletions src/components/events/EventForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ import { locationLabel } from "utils/formatEvent";

const admin_roles = ["cc", "slo"];

const poster_maxSizeMB = 10;
const poster_warnSizeMB = 1;

export default function EventForm({
id = null,
defaultValues = {},
Expand Down Expand Up @@ -252,20 +255,11 @@ export default function EventForm({
if (typeof formData.poster === "string") {
data.poster = formData.poster;
} else if (Array.isArray(formData.poster) && formData.poster.length > 0) {
const { filename, underlimit } = await uploadImageFile(
data.poster = await uploadImageFile(
formData.poster[0],
poster_filename,
poster_warnSizeMB,
);
if (!underlimit) {
triggerToast({
title: "Warning",
messages: [
"Poster FileSize exceeds the maximum limit of 0.3 MB, might affect quality during compression.",
],
severity: "warning",
});
}
data.poster = filename;
} else {
data.poster = null;
}
Expand Down Expand Up @@ -538,9 +532,9 @@ export default function EventForm({
label="Poster"
control={control}
maxFiles={1}
maxSizeMB={10}
maxSizeMB={poster_maxSizeMB}
shape="square"
warnSizeMB={1}
warnSizeMB={poster_warnSizeMB}
/>
</Grid>
</Grid>
Expand Down Expand Up @@ -1428,7 +1422,7 @@ function EventPOC({
) : members.length === 0 ? (
<TextField
disabled
value="No members found. Please add members to the club/body."
value="No approved current members found. Please add members to the club/body or wait for approval."
variant="outlined"
fullWidth
/>
Expand Down
5 changes: 3 additions & 2 deletions src/components/profile/UserForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { uploadImageFile } from "utils/files";
import { updateUserDataAction } from "actions/users/save/server_action";

const profile_warnSizeMB = 0.5;
const profile_maxSizeMB = 5;

export default function UserForm({ defaultValues = {}, action = "log" }) {
const router = useRouter();
Expand Down Expand Up @@ -219,10 +220,10 @@ export default function UserForm({ defaultValues = {}, action = "log" }) {
name="img"
label="Profile Image"
control={control}
maxFiles={1}
maxSizeMB={5}
shape="circle"
maxFiles={1}
warnSizeMB={profile_warnSizeMB}
maxSizeMB={profile_maxSizeMB}
/>
</Grid>
</Grid>
Expand Down
29 changes: 13 additions & 16 deletions src/utils/files.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function getNginxFile(filepath) {
export function getStaticFile(
filepath,
filetype = "image",
public_url = false,
public_url = false
) {
if (filepath?.toLowerCase()?.endsWith("pdf")) {
filetype = "pdf";
Expand Down Expand Up @@ -49,33 +49,30 @@ export async function uploadImageFile(file, filename = null, maxSizeMB = 0.3) {
// construct filename
const ext = file.name.split(".").pop();
filename = filename ? `${filename}.${ext}` : file.name;
let underlimit = true;

// check file size limits
const sizeLimit = maxSizeMB * (1024 * 1024);
if (file.size > sizeLimit) {
underlimit = false;
}

return await {
underlimit: underlimit,
filename: await uploadFileCommon(file, "image", false, filename, maxSizeMB),
};
const finalFilename = await uploadFileCommon(
file,
"image",
false,
filename,
maxSizeMB
);
return finalFilename;
}

export async function uploadPDFFile(
file,
static_file = false,
title = null,
maxSizeMB = 20,
maxSizeMB = 20
) {
if (!file || !title) return null;

// check file size limits
const sizeLimit = maxSizeMB * (1024 * 1024);
if (file.size > sizeLimit) {
throw new Error(
`File size exceeded ${maxSizeMB} MB, Please compress and reupload.`,
`File size exceeded ${maxSizeMB} MB, Please compress and reupload.`
);
}

Expand All @@ -90,7 +87,7 @@ export async function uploadPDFFile(
"document",
static_file,
filename,
maxSizeMB,
maxSizeMB
);
return finalFilename;
}
Expand All @@ -100,7 +97,7 @@ async function uploadFileCommon(
filetype,
static_file = false,
filename = null,
maxSizeMB = 0.3,
maxSizeMB = 0.3
) {
try {
// get signed url
Expand Down

0 comments on commit 55b7211

Please sign in to comment.