Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
appbase-edge-function
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
duanjinfei
appbase-edge-function
Commits
6b7c1fbb
Commit
6b7c1fbb
authored
Nov 26, 2024
by
duanjinfei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix update app
parent
5fbd4b46
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
69 additions
and
32 deletions
+69
-32
index.ts
supabase/functions/update-app/index.ts
+69
-32
No files found.
supabase/functions/update-app/index.ts
View file @
6b7c1fbb
...
@@ -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
,
});
});
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment