1
1
import express , { Request , Response } from "express" ;
2
2
3
3
import { needAdmin } from "../auth/authRequiredMiddleware.js" ;
4
- import { sampleCategories , sampleImages } from "../sampleData.js" ;
4
+ import { parseSampleTags , sampleCategories , sampleImages } from "../sampleData.js" ;
5
+ import { useDatabase } from "../db/database.js" ;
6
+ import { uploadFile } from "../files/s3.js" ;
5
7
6
8
export const router = express . Router ( ) ;
7
9
@@ -18,3 +20,53 @@ router.get("/", needAdmin, async (req: Request, res: Response) => {
18
20
} ) ,
19
21
} ) ;
20
22
} ) ;
23
+
24
+ router . post ( "/import" , needAdmin , async ( req : Request , res : Response ) => {
25
+ const categoriesToImport : string [ ] = req . body . categories || [ ] ;
26
+ const imagesToImport : string [ ] = req . body . images || [ ] ;
27
+
28
+ const db = useDatabase ( ) ;
29
+
30
+ for ( const name of categoriesToImport ) {
31
+ const categoryData = sampleCategories . filter ( ( info ) => info . name === name ) . pop ( ) ;
32
+ if ( ! categoryData ) {
33
+ continue ;
34
+ }
35
+
36
+ console . log ( "Importing sample category" , categoryData . name ) ;
37
+ let cImage = - 1 ;
38
+ if ( categoryData . cover ) {
39
+ const finalPath = `samples/covers/${ categoryData . cover } ` ;
40
+ cImage = ( await db . addImage ( finalPath , "" , "" , "" ) ) || cImage ;
41
+ await uploadFile ( `./samples/covers/${ categoryData . cover } ` , finalPath ) ;
42
+ }
43
+ const cId = await db . addCategory ( categoryData . id , categoryData . name , cImage , categoryData . tags || [ ] ) ;
44
+ if ( ! cId ) {
45
+ continue ;
46
+ }
47
+
48
+ for ( const author of imagesToImport ) {
49
+ const imageProviderData = sampleImages . filter ( ( info ) => info . author === author ) . pop ( ) ;
50
+ if ( ! imageProviderData ) {
51
+ continue ;
52
+ }
53
+
54
+ for ( const imageList of imageProviderData . images ) {
55
+ if ( imageList . category === categoryData . id ) {
56
+ for ( const image of imageList . images ) {
57
+ const finalPath = `samples/${ image . path } ` ;
58
+ await uploadFile ( `./samples/${ image . path } ` , finalPath ) ;
59
+ const iId = await db . addImage ( finalPath , "" , imageProviderData . author , imageProviderData . author_url ) ;
60
+ if ( iId ) {
61
+ await db . addImageToCategory ( cId , iId , parseSampleTags ( image . tags || [ ] ) ) ;
62
+ }
63
+ }
64
+ }
65
+ }
66
+ }
67
+ }
68
+
69
+ res . json ( {
70
+ ok : true ,
71
+ } ) ;
72
+ } ) ;
0 commit comments