Commit d8e24864 authored by Aravind RK's avatar Aravind RK

feat : added file type for uploading

parent e01f6a70
...@@ -43,8 +43,8 @@ ...@@ -43,8 +43,8 @@
allowedHeaders: ['Content-Type', 'Authorization'] allowedHeaders: ['Content-Type', 'Authorization']
})); }));
app.use(express.json()); app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: true })); app.use(express.urlencoded({ extended: true, limit: '10mb' }));
app.use(cookieParser()); app.use(cookieParser());
// Serve static files from uploads folder (for resumes, etc) // Serve static files from uploads folder (for resumes, etc)
......
...@@ -47,6 +47,7 @@ ...@@ -47,6 +47,7 @@
<input type="file" accept="image/*" style="display: none;" (change)="uploadSignature($event)"> <input type="file" accept="image/*" style="display: none;" (change)="uploadSignature($event)">
</label> </label>
</div> </div>
<p style="margin-top: 8px; font-size: 12px; color: #666;">Valid formats: JPG, PNG, GIF, WebP (Max 5MB)</p>
} @else { } @else {
<div class="empty-state" style="padding: 40px 20px;"> <div class="empty-state" style="padding: 40px 20px;">
<span class="material-symbols-rounded">draw</span> <span class="material-symbols-rounded">draw</span>
...@@ -57,6 +58,7 @@ ...@@ -57,6 +58,7 @@
@else { <span class="material-symbols-rounded">upload</span> Upload Signature } @else { <span class="material-symbols-rounded">upload</span> Upload Signature }
<input type="file" accept="image/*" style="display: none;" (change)="uploadSignature($event)"> <input type="file" accept="image/*" style="display: none;" (change)="uploadSignature($event)">
</label> </label>
<p style="margin-top: 8px; font-size: 12px; color: #666;">Valid formats: JPG, PNG, GIF, WebP (Max 5MB)</p>
</div> </div>
} }
</div> </div>
......
...@@ -85,27 +85,72 @@ export class StaffProfileComponent implements OnInit { ...@@ -85,27 +85,72 @@ export class StaffProfileComponent implements OnInit {
const file = event.target.files[0]; const file = event.target.files[0];
if (!file) return; if (!file) return;
// Validate file type
const allowedTypes = ['image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/webp'];
if (!allowedTypes.includes(file.type)) {
alert('Please upload an image file (JPG, PNG, GIF, or WebP)');
return;
}
// Validate file size (max 5MB before compression)
if (file.size > 5 * 1024 * 1024) {
alert('File size must be less than 5MB');
return;
}
const reader = new FileReader(); const reader = new FileReader();
reader.onload = () => { reader.onload = (e: any) => {
const base64String = reader.result as string; const img = new Image();
this.isUploadingSignature.set(true); img.onload = () => {
// Resize to signature-appropriate dimensions and compress as JPEG
this.authService.uploadSignature({ signature: base64String }).subscribe({ const MAX_WIDTH = 600;
next: (res) => { const MAX_HEIGHT = 200;
this.isUploadingSignature.set(false); let width = img.width;
if (res.signature) { let height = img.height;
// Update user signal
this.user.update(u => ({ ...u, signature: res.signature })); // Scale down proportionally if needed
} if (width > MAX_WIDTH) {
}, height = Math.round((height * MAX_WIDTH) / width);
error: (err) => { width = MAX_WIDTH;
this.isUploadingSignature.set(false); }
alert(err.error?.message || 'Error uploading signature'); if (height > MAX_HEIGHT) {
width = Math.round((width * MAX_HEIGHT) / height);
height = MAX_HEIGHT;
} }
});
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d')!;
// White background so transparent PNGs look clean
ctx.fillStyle = '#ffffff';
ctx.fillRect(0, 0, width, height);
ctx.drawImage(img, 0, 0, width, height);
// Export as JPEG at 80% quality (much smaller than raw PNG base64)
const base64String = canvas.toDataURL('image/jpeg', 0.8);
this.isUploadingSignature.set(true);
this.authService.uploadSignature({ signature: base64String }).subscribe({
next: (res) => {
this.isUploadingSignature.set(false);
if (res.signature) {
this.user.update(u => ({ ...u, signature: res.signature }));
}
},
error: (err) => {
this.isUploadingSignature.set(false);
alert(err.error?.message || 'Error uploading signature. Please try again.');
}
});
};
img.onerror = () => {
alert('Failed to load image. Please try a different file.');
};
img.src = e.target.result;
}; };
reader.onerror = () => { reader.onerror = () => {
alert('Failed to read file'); alert('Failed to read file. Please try again.');
}; };
reader.readAsDataURL(file); reader.readAsDataURL(file);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment