forked from ChatGPTNextWeb/ChatGPT-Next-Web
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ChatGPTNextWeb#5061 from ConnectAI-E/feature-cache…
…-storage using cache storage store image data ChatGPTNextWeb#5013
- Loading branch information
Showing
9 changed files
with
196 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,27 @@ | ||
if ('serviceWorker' in navigator) { | ||
window.addEventListener('load', function () { | ||
window.addEventListener('DOMContentLoaded', function () { | ||
navigator.serviceWorker.register('/serviceWorker.js').then(function (registration) { | ||
console.log('ServiceWorker registration successful with scope: ', registration.scope); | ||
const sw = registration.installing || registration.waiting | ||
if (sw) { | ||
sw.onstatechange = function() { | ||
if (sw.state === 'installed') { | ||
// SW installed. Reload for SW intercept serving SW-enabled page. | ||
console.log('ServiceWorker installed reload page'); | ||
window.location.reload(); | ||
} | ||
} | ||
} | ||
registration.update().then(res => { | ||
console.log('ServiceWorker registration update: ', res); | ||
}); | ||
window._SW_ENABLED = true | ||
}, function (err) { | ||
console.error('ServiceWorker registration failed: ', err); | ||
}); | ||
navigator.serviceWorker.addEventListener('controllerchange', function() { | ||
console.log('ServiceWorker controllerchange '); | ||
window.location.reload(true); | ||
}); | ||
}); | ||
} | ||
} |