-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate.js
More file actions
163 lines (140 loc) · 5.62 KB
/
migrate.js
File metadata and controls
163 lines (140 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const fs = require('fs');
const path = require('path');
const fetch = require('node-fetch');
const FormData = require('form-data');
require('dotenv').config();
const API_URL = process.env.SPIDER_API_URL || 'http://192.168.100.164:3006/api/v1';
const API_KEY = process.env.SPIDER_API_KEY || '20ed16fd2f5001ebd7f5fa01f73e82f90abd9d99db4e2e24afa0954342008e8b';
const DB_NAME = process.env.SPIDER_DB_NAME || 'sw_Franco Calegari_spiderweb';
const STORAGE_PROJECT_ID = process.env.SPIDER_STORAGE_PROJECT_ID || '5';
const HEADERS = {
'X-API-KEY': API_KEY,
'Content-Type': 'application/json',
};
async function spiderQuery(query, params = []) {
let finalQuery = query;
if (params.length > 0) {
let i = 0;
finalQuery = query.replace(/\?/g, () => {
const val = params[i++];
if (val === null || val === undefined) return "NULL";
if (typeof val === "number") return val;
return `'${String(val).replace(/'/g, "''")}'`;
});
}
const res = await fetch(`${API_URL}/query`, {
method: "POST",
headers: HEADERS,
body: JSON.stringify({ database: DB_NAME, query: finalQuery }),
});
const json = await res.json();
if (!json.success) {
throw new Error(`Spider SQL error: ${json.error || JSON.stringify(json)}`);
}
return json.result;
}
// Function to determine MIME type based on file extension
function getMimeType(filePath) {
const ext = path.extname(filePath).toLowerCase();
switch (ext) {
case '.jpg':
case '.jpeg':
return 'image/jpeg';
case '.png':
return 'image/png';
case '.webp':
return 'image/webp';
case '.gif':
return 'image/gif';
default:
return 'application/octet-stream'; // Default if unknown
}
}
async function spiderUpload(filePath) {
if (!filePath) return { fileUrl: null, fileId: null };
// Resolve the path relative to the public directory
const resolvedPath = path.join(__dirname, 'public', filePath.replace(/^\.\//, ''));
if (!fs.existsSync(resolvedPath)) {
console.warn(`[WARN] File not found: ${resolvedPath}. Skipping upload.`);
return { fileUrl: null, fileId: null };
}
const buffer = fs.readFileSync(resolvedPath);
const originalname = path.basename(resolvedPath);
const mimetype = getMimeType(resolvedPath);
const form = new FormData();
form.append("files", buffer, { filename: originalname, contentType: mimetype });
const res = await fetch(
`${API_URL}/storage/projects/${STORAGE_PROJECT_ID}/files`,
{
method: "POST",
headers: { "X-API-KEY": API_KEY, ...form.getHeaders() },
body: form,
}
);
const json = await res.json();
if (!json.success) {
throw new Error(`Spider upload error: ${json.error || JSON.stringify(json)}`);
}
const file = json.files[0];
return {
fileId: String(file.id),
fileUrl: `${API_URL}/storage/files/${file.id}`,
};
}
async function migrateData() {
const dataFilePath = path.join(__dirname, 'public', 'assets', 'data', 'data.json');
if (!fs.existsSync(dataFilePath)) {
console.error(`Data file not found at ${dataFilePath}`);
return;
}
const data = JSON.parse(fs.readFileSync(dataFilePath, 'utf8'));
console.log(`Starting migration...`);
// Migrate Projects
if (data.projects && data.projects.length > 0) {
console.log(`Migrating ${data.projects.length} projects...`);
for (const project of data.projects) {
console.log(` - Processing project: ${project.title}`);
const uploaded = await spiderUpload(project.image);
await spiderQuery(
"INSERT INTO projects (title, description, image_url, image_file_id, url) VALUES (?, ?, ?, ?, ?)",
[project.title, project.description || null, uploaded.fileUrl, uploaded.fileId, project.url || null]
);
}
}
// Migrate Sponsors
if (data.sponsors && data.sponsors.length > 0) {
console.log(`Migrating ${data.sponsors.length} sponsors...`);
for (const sponsor of data.sponsors) {
console.log(` - Processing sponsor: ${sponsor.name}`);
const uploaded = await spiderUpload(sponsor.image);
await spiderQuery(
"INSERT INTO sponsors (name, url, image_url, image_file_id) VALUES (?, ?, ?, ?)",
[sponsor.name, sponsor.url || null, uploaded.fileUrl, uploaded.fileId]
);
}
}
// Migrate Designs
if (data.designs && data.designs.length > 0) {
console.log(`Migrating ${data.designs.length} designs...`);
for (const design of data.designs) {
console.log(` - Processing design: ${design.title}`);
const imageUploaded = await spiderUpload(design.image);
const logoUploaded = await spiderUpload(design.clientLogo);
await spiderQuery(
"INSERT INTO designs (title, description, image_url, image_file_id, client_name, client_logo_url, client_logo_file_id, link) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
[
design.title,
design.description || null,
imageUploaded.fileUrl,
imageUploaded.fileId,
design.clientName || null,
logoUploaded.fileUrl,
logoUploaded.fileId,
design.link || null
]
);
}
}
console.log('Migration completed successfully!');
}
migrateData().catch(console.error);