@@ -4,20 +4,20 @@ import { Button, Input, message, Modal, Space, Upload } from 'antd'
44import { InboxOutlined } from '@ant-design/icons'
55import { AppContext } from '../contexts/GlobalContext'
66import { DEFAULT_IMAGE } from '../utils/utils'
7- import UTIF from 'utif' ;
7+ import UTIF from 'utif'
88
99const path = require ( 'path' )
1010
1111export function Dragger ( ) {
1212 const context = useContext ( AppContext )
1313
14- const getBase64 = ( file ) =>
15- new Promise ( ( resolve , reject ) => {
16- const reader = new FileReader ( )
17- reader . readAsDataURL ( file )
18- reader . onload = ( ) => resolve ( reader . result )
19- reader . onerror = ( error ) => reject ( error )
20- } )
14+ // const getBase64 = (file) =>
15+ // new Promise((resolve, reject) => {
16+ // const reader = new FileReader()
17+ // reader.readAsDataURL(file)
18+ // reader.onload = () => resolve(reader.result)
19+ // reader.onerror = (error) => reject(error)
20+ // })
2121
2222 const onChange = ( info ) => {
2323 const { status } = info . file
@@ -118,69 +118,68 @@ export function Dragger () {
118118 const handleCancel = ( ) => setPreviewOpen ( false )
119119 // Function to generate preview for TIFF files
120120 const generateTiffPreview = ( file , callback ) => {
121- const reader = new FileReader ( ) ;
122- reader . onload = function ( event ) {
121+ const reader = new FileReader ( )
122+ reader . onload = function ( event ) {
123123 try {
124- const buffer = new Uint8Array ( event . target . result ) ;
125- console . log ( 'Buffer length: ' , buffer . length ) ; //Log buffer length in bytes
126-
127- const tiffPages = UTIF . decode ( buffer ) ;
128-
129- //Check if tiffPages array is not empty
130- if ( tiffPages . length === 0 ) throw new Error ( 'No TIFF pages found' ) ;
131-
132- const firstPage = tiffPages [ 0 ] ;
133- console . log ( 'First page before decoding:' , firstPage ) ; // Log first page object before decoding
124+ const buffer = new Uint8Array ( event . target . result )
125+ console . log ( 'Buffer length: ' , buffer . length ) // Log buffer length in bytes
126+
127+ const tiffPages = UTIF . decode ( buffer )
128+
129+ // Check if tiffPages array is not empty
130+ if ( tiffPages . length === 0 ) throw new Error ( 'No TIFF pages found' )
131+
132+ const firstPage = tiffPages [ 0 ]
133+ console . log ( 'First page before decoding:' , firstPage ) // Log first page object before decodin
134134
135135 // Ensure the firstPage has necessary tags before decoding
136- if ( ! firstPage . t256 || ! firstPage . t257 ) throw new Error ( 'First page is missing essential tags (width and height)' ) ;
137-
138- UTIF . decodeImage ( buffer , firstPage ) ; // firstPage before and after decoding, the result is same.
139- console . log ( 'TIFF first page after decoding: ' , firstPage ) ; //Log the first page object
136+ if ( ! firstPage . t256 || ! firstPage . t257 ) throw new Error ( 'First page is missing essential tags (width and height)' )
137+
138+ UTIF . decodeImage ( buffer , firstPage ) // firstPage before and after decoding, the result is same.
139+ console . log ( 'TIFF first page after decoding: ' , firstPage ) // Log the first page object
140140
141141 // Extract width and height from the TIFF tags
142- const width = firstPage . t256 ? firstPage . t256 [ 0 ] : 0 ;
143- const height = firstPage . t257 ? firstPage . t257 [ 0 ] : 0 ;
144-
142+ const width = firstPage . t256 ? firstPage . t256 [ 0 ] : 0
143+ const height = firstPage . t257 ? firstPage . t257 [ 0 ] : 0
144+
145145 // Check if width and height are valid
146146 if ( width > 0 && height > 0 ) {
147- const rgba = UTIF . toRGBA8 ( firstPage ) ; // Uint8Array with RGBA pixels
147+ const rgba = UTIF . toRGBA8 ( firstPage ) // Uint8Array with RGBA pixels
148148
149149 // Create a canvas to draw the TIFF image
150- const canvas = document . createElement ( 'canvas' ) ;
151- const ctx = canvas . getContext ( '2d' ) ;
152- canvas . width = width ;
153- canvas . height = height ;
154- const imageData = ctx . createImageData ( width , height ) ;
150+ const canvas = document . createElement ( 'canvas' )
151+ const ctx = canvas . getContext ( '2d' )
152+ canvas . width = width
153+ canvas . height = height
154+ const imageData = ctx . createImageData ( width , height )
155155
156- imageData . data . set ( rgba ) ;
157- ctx . putImageData ( imageData , 0 , 0 ) ;
156+ imageData . data . set ( rgba )
157+ ctx . putImageData ( imageData , 0 , 0 )
158158
159- const dataURL = canvas . toDataURL ( ) ;
160- console . log ( 'Canvas data URL:' , dataURL ) ;
161-
162- callback ( dataURL ) ;
159+ const dataURL = canvas . toDataURL ( )
160+ console . log ( 'Canvas data URL:' , dataURL )
163161
162+ callback ( dataURL )
164163 } else {
165- console . error ( 'TIFF image has invalid dimensions:' , { width, height } ) ;
166- message . error ( 'TIFF image has invalid dimensions.' ) ;
167- setPreviewImage ( DEFAULT_IMAGE ) ; // Fallback to default image
168- }
164+ console . error ( 'TIFF image has invalid dimensions:' , { width, height } )
165+ message . error ( 'TIFF image has invalid dimensions.' )
166+ setPreviewImage ( DEFAULT_IMAGE ) // Fallback to default image
167+ }
169168 } catch ( error ) {
170- console . error ( 'Failed to generate TIFF preview:' , error ) ;
171- message . error ( 'Failed to generate TIFF preview.' ) ;
172- setPreviewImage ( DEFAULT_IMAGE ) ; // Fallback to default image
173- }
174- } ;
175- reader . readAsArrayBuffer ( file ) ;
176- } ;
169+ console . error ( 'Failed to generate TIFF preview:' , error )
170+ message . error ( 'Failed to generate TIFF preview.' )
171+ setPreviewImage ( DEFAULT_IMAGE ) // Fallback to default image
172+ }
173+ }
174+ reader . readAsArrayBuffer ( file )
175+ }
177176
178- //When click preview eye icon, implement handlePreview function
177+ // When click preview eye icon, implement handlePreview function
179178 const handlePreview = async ( file ) => {
180- setFileUID ( file . uid ) ;
181- setPreviewOpen ( true ) ;
182- setPreviewImage ( file . thumbUrl ) ;
183- setPreviewTitle ( file . name || file . url . substring ( file . url . lastIndexOf ( '/' ) + 1 ) ) ;
179+ setFileUID ( file . uid )
180+ setPreviewOpen ( true )
181+ setPreviewImage ( file . thumbUrl )
182+ setPreviewTitle ( file . name || file . url . substring ( file . url . lastIndexOf ( '/' ) + 1 ) )
184183 if (
185184 context . files . find ( targetFile => targetFile . uid === file . uid ) &&
186185 context . files . find ( targetFile => targetFile . uid === file . uid ) . folderPath ) {
@@ -201,24 +200,24 @@ export function Dragger () {
201200 // when click or drag file to this area to upload, below function will be deployed.
202201 const handleBeforeUpload = ( file ) => {
203202 // Create a URL for the thumbnail using object URL
204- if ( file . type === " image/tiff" || file . type === " image/tif" ) {
203+ if ( file . type === ' image/tiff' || file . type === ' image/tif' ) {
205204 return new Promise ( ( resolve ) => {
206205 generateTiffPreview ( file , ( dataURL ) => {
207- file . thumbUrl = dataURL ;
208- console . log ( 'file thumbUrl inside callback is' , file . thumbUrl ) ;
206+ file . thumbUrl = dataURL
207+ console . log ( 'file thumbUrl inside callback is' , file . thumbUrl )
209208 resolve ( file )
210- } ) ;
211- console . log ( 'file thumbUrl is' , file . thumbUrl ) ;
209+ } )
210+ console . log ( 'file thumbUrl is' , file . thumbUrl )
212211 } )
213212 } else {
214- file . thumbUrl = URL . createObjectURL ( file ) ;
213+ file . thumbUrl = URL . createObjectURL ( file )
215214 }
216215 return true // Allow the upload
217216 }
218217
219218 return (
220219 < >
221- < Upload . Dragger
220+ < Upload . Dragger
222221 multiple
223222 onChange = { onChange }
224223 customRequest = { uploadImage }
@@ -276,4 +275,3 @@ export function Dragger () {
276275 </ >
277276 )
278277}
279-
0 commit comments