@@ -2,16 +2,17 @@ import fs from 'fs';
22
33import { fetchURL , write } from 'image-js' ;
44
5- import { defaultImages , defaultMasks } from './imageDataset.js ' ;
5+ import { defaultImages , defaultMasks } from './imageDataset.mjs ' ;
66
77export async function imageLoader ( ) {
8- const staticDir = '/demoImages/' ;
8+ const demoImagesDir = 'demoImages' ;
9+ const staticDir = 'static' ;
910
1011 const imageData = { masks : [ ] , images : [ ] } ;
1112 try {
1213 // Create static directory if it doesn't exist
13- if ( ! fs . existsSync ( `./static ${ staticDir } ` ) ) {
14- fs . mkdirSync ( `./static ${ staticDir } ` , { recursive : true } ) ;
14+ if ( ! fs . existsSync ( `./${ staticDir } / ${ demoImagesDir } ` ) ) {
15+ fs . mkdirSync ( `./${ staticDir } / ${ demoImagesDir } ` , { recursive : true } ) ;
1516 }
1617
1718 const images = await Promise . all (
@@ -21,50 +22,49 @@ export async function imageLoader() {
2122 for ( let i = 0 ; i < images . length ; i ++ ) {
2223 const image = images [ i ] ;
2324 const imageDataUrl = defaultImages [ i ] ;
24- const imageTitle = imageDataUrl . value . slice (
25- imageDataUrl . value . lastIndexOf ( '/' ) + 1 ,
26- ) ;
27- write ( `./static${ staticDir } images/${ imageTitle } ` , image , {
25+ const imageTitle = getFilename ( imageDataUrl . value ) ;
26+ write ( `./${ staticDir } /${ demoImagesDir } /images/${ imageTitle } ` , image , {
2827 recursive : true ,
2928 } ) ;
30-
29+ // Keeping object structure for compatibility
3130 imageData . images . push ( {
3231 type : 'url' ,
3332 imageType : 'image' ,
3433 label : `${ imageDataUrl . label } (${ image . width } x${ image . height } )` ,
35- value : `${ staticDir } images/${ imageTitle } ` ,
34+ value : `/ ${ demoImagesDir } / images/${ imageTitle } ` ,
3635 } ) ;
3736 }
3837
39- // Fetch all masks in parallel
4038 const masks = await Promise . all (
4139 defaultMasks . map ( ( maskDataUrl ) => fetchURL ( maskDataUrl . value ) ) ,
4240 ) ;
4341
4442 for ( let i = 0 ; i < masks . length ; i ++ ) {
4543 const mask = masks [ i ] ;
4644 const maskDataUrl = defaultMasks [ i ] ;
47- const maskTitle = maskDataUrl . value . slice (
48- maskDataUrl . value . lastIndexOf ( '/' ) + 1 ,
49- ) ;
50- write ( `./static${ staticDir } masks/${ maskTitle } ` , mask , {
45+ const maskTitle = getFilename ( maskDataUrl . value ) ;
46+ write ( `./${ staticDir } /${ demoImagesDir } /masks/${ maskTitle } ` , mask , {
5147 recursive : true ,
5248 } ) ;
49+ // Keeping object structure for compatibility
5350 imageData . masks . push ( {
5451 type : 'url' ,
5552 imageType : 'mask' ,
5653 label : `${ maskDataUrl . label } (${ mask . width } x${ mask . height } )` ,
57- value : `${ staticDir } masks/${ maskTitle } ` ,
54+ value : `/ ${ demoImagesDir } / masks/${ maskTitle } ` ,
5855 } ) ;
5956 }
60-
61- const outputPath = `./static ${ staticDir } imageData.json` ;
57+ // Write data about newly created files.
58+ const outputPath = `./${ staticDir } / ${ demoImagesDir } / imageData.json` ;
6259
6360 fs . writeFileSync ( outputPath , JSON . stringify ( imageData , null , 2 ) ) ;
6461 } catch ( error ) {
6562 throw new Error ( `Error in imageLoader: ${ error . message } ` ) ;
6663 }
67- // Fetch all images in parallel
6864
6965 return imageData ;
7066}
67+ // Returns only filename with extension.
68+ function getFilename ( filepath ) {
69+ return filepath . replace ( / ^ .* [ \\ / ] / , '' ) ;
70+ }
0 commit comments