Commit 6b7c1fbb authored by duanjinfei's avatar duanjinfei

fix update app

parent 5fbd4b46
...@@ -52,24 +52,40 @@ Deno.serve(async (req) => { ...@@ -52,24 +52,40 @@ Deno.serve(async (req) => {
const timestamp = Math.floor(Date.now() / 3600000) * 3600; const timestamp = Math.floor(Date.now() / 3600000) * 3600;
// 上传所有 app 数据到 storage // 上传所有 app 数据到 storage
const allAppsJson = JSON.stringify(allApps);
const directory = `app-category/${timestamp}`; const directory = `app-category/${timestamp}`;
const allAppsFileName = `${directory}/app_all.json`;
const bucketName = 'cache'; // 替换为存储桶名称 const bucketName = 'cache'; // 替换为存储桶名称
// 分页参数
const pageSize = 30;
const totalCount = allApps.length;
const paginatedApps = [];
for (let i = 0; i < totalCount; i += pageSize) {
paginatedApps.push(allApps.slice(i, i + pageSize));
}
const { error: allAppsUploadError } = await supabase.storage // 遍历分页后的数据并上传
.from(bucketName) for (let pageNum = 1; pageNum <= paginatedApps.length; pageNum++) {
.upload(allAppsFileName, new Blob([allAppsJson]), { const pageApps = paginatedApps[pageNum - 1];
contentType: 'application/json', const pageJson = JSON.stringify({
upsert: true, total_count: totalCount,
apps: pageApps,
}); });
if (allAppsUploadError) { const pageFileName = `${directory}/app_all_${pageNum}.json`;
console.error('Error uploading all apps JSON:', allAppsUploadError);
return new Response( const { error: pageUploadError } = await supabase.storage
JSON.stringify({ error: 'Failed to upload all apps JSON' }), .from(bucketName)
{ status: 500 } .upload(pageFileName, new Blob([pageJson]), {
); contentType: 'application/json',
upsert: true,
});
if (pageUploadError) {
console.error(`Error uploading page ${pageNum} JSON:`, pageUploadError);
return new Response(
JSON.stringify({ error: `Failed to upload app_all_${pageNum}.json` }),
{ status: 500 }
);
}
} }
// 按 category_id 分组 // 按 category_id 分组
...@@ -82,28 +98,45 @@ Deno.serve(async (req) => { ...@@ -82,28 +98,45 @@ Deno.serve(async (req) => {
}); });
// 上传每个 category_id 的数据 // 上传每个 category_id 的数据
// 上传每个 category_id 的分页数据
for (const [categoryId, apps] of Object.entries(groupedData)) { for (const [categoryId, apps] of Object.entries(groupedData)) {
const categoryJson = JSON.stringify(apps); const totalCount = apps.length; // 当前分类的总数
const categoryFileName = `${directory}/${categoryId}.json`; const pageSize = 30;
const paginatedApps = [];
for (let i = 0; i < totalCount; i += pageSize) {
paginatedApps.push(apps.slice(i, i + pageSize));
}
const { error: categoryUploadError } = await supabase.storage // 遍历分页数据并上传
.from(bucketName) for (let pageNum = 1; pageNum <= paginatedApps.length; pageNum++) {
.upload(categoryFileName, new Blob([categoryJson]), { const pageApps = paginatedApps[pageNum - 1];
contentType: 'application/json', const pageJson = JSON.stringify({
upsert: true, total_count: totalCount,
apps: pageApps,
}); });
if (categoryUploadError) { // 文件名格式:{categoryId}_{pageNum}.json
console.error( const categoryPageFileName = `${directory}/${categoryId}_${pageNum}.json`;
`Error uploading category ${categoryId} JSON:`,
categoryUploadError const { error: categoryPageUploadError } = await supabase.storage
); .from(bucketName)
return new Response( .upload(categoryPageFileName, new Blob([pageJson]), {
JSON.stringify({ contentType: 'application/json',
error: `Failed to upload category ${categoryId} JSON`, upsert: true,
}), });
{ status: 500 }
); if (categoryPageUploadError) {
console.error(
`Error uploading category ${categoryId} page ${pageNum} JSON:`,
categoryPageUploadError
);
return new Response(
JSON.stringify({
error: `Failed to upload category ${categoryId} page ${pageNum} JSON`,
}),
{ status: 500 }
);
}
} }
} }
return new Response( return new Response(
...@@ -111,11 +144,15 @@ Deno.serve(async (req) => { ...@@ -111,11 +144,15 @@ Deno.serve(async (req) => {
message: 'All data uploaded successfully', message: 'All data uploaded successfully',
fileName: allAppsFileName, fileName: allAppsFileName,
}), }),
{ status: 200 } {
headers: { 'Content-Type': 'application/json' },
status: 200
}
); );
} catch (err) { } catch (err) {
console.error('Unexpected error:', err); console.error('Unexpected error:', err);
return new Response(JSON.stringify({ error: 'Internal server error' }), { return new Response(JSON.stringify({ error: 'Internal server error' }), {
headers: { 'Content-Type': 'application/json' },
status: 500, status: 500,
}); });
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment