Skip to content

Commit e63c210

Browse files
committed
Allow importing sample data
1 parent b823bf6 commit e63c210

File tree

2 files changed

+72
-7
lines changed

2 files changed

+72
-7
lines changed

src/routes/samples.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import express, { Request, Response } from "express";
22

33
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";
57

68
export const router = express.Router();
79

@@ -18,3 +20,53 @@ router.get("/", needAdmin, async (req: Request, res: Response) => {
1820
}),
1921
});
2022
});
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+
});

src/sampleData.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,34 @@
33
// appear as sample data with certain conditions applying.
44
// check the LICENSE file for details.
55

6-
import { Category, SampleProviderEntry } from "./types/drawref.js";
6+
import { Category, SampleProviderEntry, TagMap } from "./types/drawref.js";
77

88
type RawSampleCategory = {
99
name: string;
10+
cover?: string;
1011
tags: string;
1112
};
1213

1314
var rawSampleCategories: RawSampleCategory[] = [
1415
{
1516
name: "Poses",
17+
cover: "pose.jpg",
1618
tags: `
1719
Bodies: Men, Women, Other
1820
Clothing: Nude, Clothed
1921
Energy: Action, Stationary`,
2022
},
2123
{
2224
name: "Faces",
25+
cover: "head.jpg",
2326
tags: `
2427
Expression: Happy, Sad, Angry, Scared, Disgust, Surprise
2528
Facial Hair: Facial Hair, No Facial Hair
2629
Bodies: Men, Women, Other`,
2730
},
2831
{
2932
name: "Animals",
33+
cover: "animals.jpg",
3034
tags: `
3135
Species: Feline, Canine, Equine & Hooved, Avian, Aquatic, Rodents & Bunnies, Bugs and Insects, Primates
3236
Skeletons: Skeletons, Live`,
@@ -59,6 +63,7 @@ export var sampleCategories: Category[] = rawSampleCategories.map((info) => {
5963
return {
6064
id: info.name.toLowerCase(),
6165
name: info.name,
66+
cover: info.cover,
6267
tags: info.tags
6368
.trim()
6469
.split("\n")
@@ -74,11 +79,19 @@ export var sampleCategories: Category[] = rawSampleCategories.map((info) => {
7479
};
7580
});
7681

77-
export var sampleCategoryCovers = new Map<string, string>([
78-
["poses", "pose.jpg"],
79-
["faces", "head.jpg"],
80-
["animals", "animals.jpg"],
81-
]);
82+
export function parseSampleTags(input: string[]): TagMap {
83+
let tags: TagMap = {};
84+
for (const group of input) {
85+
const newGroup = group.split(" ", 2);
86+
const name = newGroup[0];
87+
const value = newGroup[1].trim();
88+
if (!tags[name]) {
89+
tags[name] = [];
90+
}
91+
tags[name].push(value);
92+
}
93+
return tags;
94+
}
8295

8396
export var sampleImages: SampleProviderEntry[] = [
8497
// adorkastock

0 commit comments

Comments
 (0)