@@ -2,6 +2,7 @@ import React, { useState } from 'react';
2
2
import { motion } from 'framer-motion' ;
3
3
import Lotte from 'lottie-react' ;
4
4
import animationData from '../assets/95241-uploading.json' ;
5
+
5
6
function UploadNotes ( ) {
6
7
const [ selectedFile , setSelectedFile ] = useState ( null ) ;
7
8
const [ uploading , setUploading ] = useState ( false ) ;
@@ -11,55 +12,67 @@ function UploadNotes() {
11
12
setSelectedFile ( event . target . files [ 0 ] ) ;
12
13
} ;
13
14
14
- const handleUpload = async ( ) => {
15
+ const handleUpload = ( ) => {
15
16
if ( ! selectedFile ) {
16
17
return ;
17
18
}
18
-
19
+
19
20
setUploading ( true ) ;
20
-
21
- // Generate a pre-signed URL for file upload
22
- const url = await getPresignedUrl ( selectedFile . name , selectedFile . type ) ;
23
-
24
- try {
25
- // Use the generated URL to upload the file directly to S3
26
- await uploadFile ( url , selectedFile ) ;
27
- setUploadSuccess ( true ) ;
28
- } catch ( error ) {
29
- console . error ( 'Error uploading file:' , error ) ;
30
- }
31
-
32
- setUploading ( false ) ;
33
- } ;
34
-
35
- // Function to generate a pre-signed URL for file upload
36
- const getPresignedUrl = async ( filename , filetype ) => {
37
- const response = await fetch ( `/api/get-upload-url?filename=${ filename } &filetype=${ filetype } ` ) ;
38
- const data = await response . json ( ) ;
39
- return data . url ;
21
+
22
+ const reader = new FileReader ( ) ;
23
+
24
+ reader . onload = ( ) => {
25
+ // Get the file contents
26
+ const fileContents = reader . result ;
27
+
28
+ // Create a Blob from the file contents
29
+ const blob = new Blob ( [ fileContents ] , { type : selectedFile . type } ) ;
30
+
31
+ // Create a file path within the Local_Storage/notes_pdf folder
32
+ const filePath = `Local_Storage/notes_pdf/${ selectedFile . name } ` ;
33
+
34
+ // Save the file locally
35
+ saveFileLocally ( filePath , blob )
36
+ . then ( ( ) => {
37
+ setUploadSuccess ( true ) ;
38
+ setUploading ( false ) ;
39
+ } )
40
+ . catch ( ( error ) => {
41
+ console . error ( 'Error saving file:' , error ) ;
42
+ } ) ;
43
+ } ;
44
+
45
+ reader . readAsArrayBuffer ( selectedFile ) ;
40
46
} ;
41
-
42
- // Function to upload the file to the generated URL
43
- const uploadFile = async ( url , file ) => {
44
- await fetch ( url , {
45
- method : 'PUT' ,
46
- body : file ,
47
- headers : {
48
- 'Content-Type' : file . type ,
49
- } ,
47
+
48
+ const saveFileLocally = ( filePath , file ) => {
49
+ return new Promise ( ( resolve , reject ) => {
50
+ const virtualLink = document . createElement ( 'a' ) ;
51
+ virtualLink . href = URL . createObjectURL ( file ) ;
52
+ virtualLink . download = filePath ;
53
+ virtualLink . addEventListener ( 'load' , ( ) => {
54
+ URL . revokeObjectURL ( virtualLink . href ) ;
55
+ resolve ( ) ;
56
+ } ) ;
57
+ virtualLink . addEventListener ( 'error' , ( error ) => {
58
+ reject ( error ) ;
59
+ } ) ;
60
+ document . body . appendChild ( virtualLink ) ;
61
+ virtualLink . click ( ) ;
62
+ document . body . removeChild ( virtualLink ) ;
50
63
} ) ;
51
64
} ;
65
+
52
66
53
67
return (
54
68
< div className = "flex flex-col items-center justify-center h-screen text-center" >
55
-
56
69
< motion . div
57
70
className = "bg-blue-500 text-white py-6 px-6 rounded-lg shadow-lg"
58
71
initial = { { scale : 0 } }
59
72
animate = { { scale : 1 } }
60
73
transition = { { duration : 0.5 } }
61
74
>
62
- < Lotte animationData = { animationData } style = { { width : 400 , height : 300 } } />
75
+ < Lotte animationData = { animationData } style = { { width : 400 , height : 300 } } />
63
76
< h1 className = "text-3xl font-bold mb-4" > Upload Notes</ h1 >
64
77
{ ! uploadSuccess ? (
65
78
< >
@@ -96,4 +109,4 @@ function UploadNotes() {
96
109
) ;
97
110
}
98
111
99
- export default UploadNotes ;
112
+ export default UploadNotes ;
0 commit comments