96 lines
2.9 KiB
TypeScript
96 lines
2.9 KiB
TypeScript
import * as SQLite from 'expo-sqlite';
|
|
|
|
const db = SQLite.openDatabaseSync('upload_status.db');
|
|
|
|
export type UploadTask = {
|
|
uri: string;
|
|
filename: string;
|
|
status: 'pending' | 'uploading' | 'success' | 'failed' | 'skipped';
|
|
progress: number; // 0-100
|
|
file_id?: string; // 后端返回的文件ID
|
|
};
|
|
|
|
// 初始化表
|
|
export function initUploadTable() {
|
|
console.log('Initializing upload tasks table...');
|
|
db.execSync(`
|
|
CREATE TABLE IF NOT EXISTS upload_tasks (
|
|
uri TEXT PRIMARY KEY NOT NULL,
|
|
filename TEXT NOT NULL,
|
|
status TEXT NOT NULL DEFAULT 'pending',
|
|
progress INTEGER NOT NULL DEFAULT 0,
|
|
file_id TEXT
|
|
);
|
|
`);
|
|
console.log('Upload tasks table initialized');
|
|
}
|
|
|
|
// 插入新的上传任务
|
|
export async function insertUploadTask(uri: string, filename: string): Promise<void> {
|
|
console.log('Inserting upload task:', uri, filename);
|
|
db.runSync(
|
|
'INSERT OR IGNORE INTO upload_tasks (uri, filename, status, progress) VALUES (?, ?, ?, ?);',
|
|
uri,
|
|
filename,
|
|
'pending',
|
|
0
|
|
);
|
|
}
|
|
|
|
// 检查文件是否已上传或正在上传
|
|
export async function getUploadTaskStatus(uri: string): Promise<UploadTask | null> {
|
|
console.log('Checking upload task status for:', uri);
|
|
const result = db.getFirstSync<UploadTask>(
|
|
'SELECT uri, filename, status, progress, file_id FROM upload_tasks WHERE uri = ?;',
|
|
uri
|
|
);
|
|
return result || null;
|
|
}
|
|
|
|
// 更新上传任务的状态
|
|
export async function updateUploadTaskStatus(uri: string, status: UploadTask['status'], file_id?: string): Promise<void> {
|
|
console.log('Updating upload task status:', uri, status, file_id);
|
|
if (file_id) {
|
|
db.runSync(
|
|
'UPDATE upload_tasks SET status = ?, file_id = ? WHERE uri = ?;',
|
|
status,
|
|
file_id,
|
|
uri
|
|
);
|
|
} else {
|
|
db.runSync(
|
|
'UPDATE upload_tasks SET status = ? WHERE uri = ?;',
|
|
status,
|
|
uri
|
|
);
|
|
}
|
|
}
|
|
|
|
// 更新上传任务的进度
|
|
export async function updateUploadTaskProgress(uri: string, progress: number): Promise<void> {
|
|
console.log('Updating upload task progress:', uri, progress);
|
|
db.runSync(
|
|
'UPDATE upload_tasks SET progress = ? WHERE uri = ?;',
|
|
progress,
|
|
uri
|
|
);
|
|
}
|
|
|
|
// 获取所有上传任务
|
|
export async function getUploadTasks(): Promise<UploadTask[]> {
|
|
console.log('Fetching all upload tasks...');
|
|
const results = db.getAllSync<UploadTask>(
|
|
'SELECT uri, filename, status, progress, file_id FROM upload_tasks;'
|
|
);
|
|
return results;
|
|
}
|
|
|
|
// 清理已完成或失败的任务 (可选,根据需求添加)
|
|
export async function cleanUpUploadTasks(): Promise<void> {
|
|
console.log('Cleaning up completed/failed upload tasks...');
|
|
db.runSync(
|
|
"DELETE FROM upload_tasks WHERE status = 'success' OR status = 'failed' OR status = 'skipped';"
|
|
);
|
|
}
|
|
|