-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_from_php.ts
More file actions
123 lines (101 loc) · 3.49 KB
/
migrate_from_php.ts
File metadata and controls
123 lines (101 loc) · 3.49 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
import { config } from 'dotenv';
import fs from 'fs/promises';
import { Kysely, MysqlDialect } from 'kysely';
import { createPool } from 'mysql2';
import path from 'path';
import { v4 as uuidv4 } from 'uuid';
import type { DB } from './src/types/database';
config();
const { DATABASE_IP, DATABASE_PORT, DATABASE_USER, DATABASE_PASSWORD, DATABASE_NAME } = process.env;
if (!DATABASE_IP || !DATABASE_PORT || !DATABASE_USER || !DATABASE_PASSWORD || !DATABASE_NAME) {
console.error('Missing database configuration in .env');
process.exit(1);
}
const dialect = new MysqlDialect({
pool: createPool({
host: DATABASE_IP,
port: parseInt(DATABASE_PORT),
user: DATABASE_USER,
password: DATABASE_PASSWORD,
database: DATABASE_NAME
})
});
const db = new Kysely<DB>({
dialect
});
const args = process.argv.slice(2);
const sourceDir = args[0];
const userId = args[1] ? parseInt(args[1]) : null;
if (!sourceDir) {
console.error('Usage: tsx migrate_from_php.ts <source_directory> [user_id]');
process.exit(1);
}
const UPLOADS_DIR = path.resolve('uploads');
const MIME_TYPES: Record<string, string> = {
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.png': 'image/png',
'.gif': 'image/gif',
'.webp': 'image/webp',
'.svg': 'image/svg+xml',
'.mp4': 'video/mp4',
'.webm': 'video/webm',
'.mov': 'video/quicktime',
'.avi': 'video/x-msvideo',
'.pdf': 'application/pdf',
'.txt': 'text/plain',
'.zip': 'application/zip',
'.rar': 'application/x-rar-compressed',
'.7z': 'application/x-7z-compressed',
'.tar': 'application/x-tar',
'.gz': 'application/gzip',
'.mp3': 'audio/mpeg',
'.wav': 'audio/wav',
'.ogg': 'audio/ogg'
};
function getMimeType(ext: string): string {
return MIME_TYPES[ext.toLowerCase()] || 'application/octet-stream';
}
async function migrate() {
try {
await fs.mkdir(UPLOADS_DIR, { recursive: true });
const files = await fs.readdir(sourceDir);
console.log(`Found ${files.length} files in ${sourceDir}`);
let count = 0;
for (const file of files) {
if (file.startsWith('.')) continue; // Skip hidden files
const filePath = path.join(sourceDir, file);
const stats = await fs.stat(filePath);
if (stats.isDirectory()) continue;
const ext = path.extname(file);
const mimeType = getMimeType(ext);
const id = uuidv4();
const newFilename = `${id}${ext}`;
const newPath = path.join(UPLOADS_DIR, newFilename);
// Use mtime if birthtime is invalid (common on Linux)
const createdAt = stats.birthtime.getTime() === 0 ? stats.mtime : stats.birthtime;
console.log(
`[${++count}/${files.length}] Migrating ${file} -> ${newFilename} (${mimeType})`
);
// Copy file instead of move to be safe
await fs.copyFile(filePath, newPath);
await db
.insertInto('files')
.values({
id,
original_name: file,
mime_type: mimeType,
size: stats.size,
uploaded_by: userId,
upload_date: createdAt
})
.execute();
}
console.log('Migration completed successfully');
} catch (error) {
console.error('Migration failed:', error);
} finally {
await db.destroy();
}
}
migrate();