forked from ChatGPTNextWeb/ChatGPT-Next-Web
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 1. using cache storage store image data; 2. get base64image bef…
…ore chat to api ChatGPTNextWeb#5013
- Loading branch information
Showing
9 changed files
with
164 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,59 @@ | ||
const CHATGPT_NEXT_WEB_CACHE = "chatgpt-next-web-cache"; | ||
const CHATGPT_NEXT_WEB_FILE_CACHE = "chatgpt-next-web-file"; | ||
let a="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";let nanoid=(e=21)=>{let t="",r=crypto.getRandomValues(new Uint8Array(e));for(let n=0;n<e;n++)t+=a[63&r[n]];return t}; | ||
|
||
self.addEventListener("activate", function (event) { | ||
console.log("ServiceWorker activated."); | ||
}); | ||
|
||
self.addEventListener("install", function (event) { | ||
self.skipWaiting(); // enable new version | ||
event.waitUntil( | ||
caches.open(CHATGPT_NEXT_WEB_CACHE).then(function (cache) { | ||
return cache.addAll([]); | ||
}), | ||
); | ||
}); | ||
|
||
self.addEventListener("fetch", (e) => {}); | ||
async function upload(request, url) { | ||
const formData = await request.formData() | ||
const file = formData.getAll('file')[0] | ||
let ext = file.name.split('.').pop() | ||
if (ext === 'blob') { | ||
ext = file.type.split('/').pop() | ||
} | ||
const fileUrl = `${url.origin}/api/cache/${nanoid()}.${ext}` | ||
// console.debug('file', file, fileUrl, request) | ||
const cache = await caches.open(CHATGPT_NEXT_WEB_FILE_CACHE) | ||
await cache.put(new Request(fileUrl), new Response(file, { | ||
headers: { | ||
'content-type': file.type, | ||
'content-length': file.size, | ||
'cache-control': 'no-cache', // file already store in disk | ||
'server': 'ServiceWorker', | ||
} | ||
})) | ||
return Response.json({ code: 0, data: fileUrl }) | ||
} | ||
|
||
async function remove(request, url) { | ||
const cache = await caches.open(CHATGPT_NEXT_WEB_FILE_CACHE) | ||
const res = await cache.delete(request.url) | ||
return Response.json({ code: 0 }) | ||
} | ||
|
||
self.addEventListener("fetch", (e) => { | ||
const url = new URL(e.request.url); | ||
if (/^\/api\/cache/.test(url.pathname)) { | ||
if ('GET' == e.request.method) { | ||
e.respondWith(caches.match(e.request)) | ||
} | ||
if ('POST' == e.request.method) { | ||
e.respondWith(upload(e.request, url)) | ||
} | ||
if ('DELETE' == e.request.method) { | ||
e.respondWith(remove(e.request, url)) | ||
} | ||
} | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters