Add google drive support

This commit is contained in:
ZXY101
2024-03-13 09:35:44 +02:00
parent bec5e84067
commit 491b9603af
6 changed files with 291 additions and 13 deletions

123
package-lock.json generated
View File

@@ -8,6 +8,8 @@
"name": "z-reader", "name": "z-reader",
"version": "0.9.0", "version": "0.9.0",
"dependencies": { "dependencies": {
"@types/gapi": "^0.0.47",
"@types/google.accounts": "^0.0.14",
"@vercel/analytics": "^1.1.0", "@vercel/analytics": "^1.1.0",
"@zip.js/zip.js": "^2.7.20", "@zip.js/zip.js": "^2.7.20",
"dexie": "^4.0.1-alpha.25", "dexie": "^4.0.1-alpha.25",
@@ -17,6 +19,7 @@
"devDependencies": { "devDependencies": {
"@sveltejs/adapter-auto": "^2.0.0", "@sveltejs/adapter-auto": "^2.0.0",
"@sveltejs/kit": "^1.20.4", "@sveltejs/kit": "^1.20.4",
"@types/gapi.client.drive-v3": "^0.0.4",
"@typescript-eslint/eslint-plugin": "^5.45.0", "@typescript-eslint/eslint-plugin": "^5.45.0",
"@typescript-eslint/parser": "^5.45.0", "@typescript-eslint/parser": "^5.45.0",
"autoprefixer": "^10.4.14", "autoprefixer": "^10.4.14",
@@ -601,6 +604,26 @@
"integrity": "sha512-XPSJHWmi394fuUuzDnGz1wiKqWfo1yXecHQMRf2l6hztTO+nPru658AyDngaBe7isIxEkRsPR3FZh+s7iVa4Uw==", "integrity": "sha512-XPSJHWmi394fuUuzDnGz1wiKqWfo1yXecHQMRf2l6hztTO+nPru658AyDngaBe7isIxEkRsPR3FZh+s7iVa4Uw==",
"dev": true "dev": true
}, },
"node_modules/@maxim_mazurok/gapi.client.discovery-v1": {
"version": "0.1.20200806",
"resolved": "https://registry.npmjs.org/@maxim_mazurok/gapi.client.discovery-v1/-/gapi.client.discovery-v1-0.1.20200806.tgz",
"integrity": "sha512-Wl6UfmZVDdWbY3PUu8E2ULk9RPLjnMqp/iOA4tcK8Ne+U/GmlnWP/e34IaZNGArfl7iXJNOG+/3Rj9L9jQyF9Q==",
"dev": true,
"dependencies": {
"@types/gapi.client": "*",
"@types/gapi.client.discovery-v1": "*"
}
},
"node_modules/@maxim_mazurok/gapi.client.drive-v3": {
"version": "0.0.20240304",
"resolved": "https://registry.npmjs.org/@maxim_mazurok/gapi.client.drive-v3/-/gapi.client.drive-v3-0.0.20240304.tgz",
"integrity": "sha512-PuGvDvRE70XsyUqnQCq5M2VJ7/E7u5pDvHZfBXunVlf5mYmkyAdbZg+ygJDJ2243wWpUXjmEcuxLDY2yamIq4g==",
"dev": true,
"dependencies": {
"@types/gapi.client": "*",
"@types/gapi.client.discovery-v1": "*"
}
},
"node_modules/@nodelib/fs.scandir": { "node_modules/@nodelib/fs.scandir": {
"version": "2.1.5", "version": "2.1.5",
"resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz",
@@ -747,6 +770,40 @@
"integrity": "sha512-LG4opVs2ANWZ1TJoKc937iMmNstM/d0ae1vNbnBvBhqCSezgVUOzcLCqbI5elV8Vy6WKwKjaqR+zO9VKirBBCA==", "integrity": "sha512-LG4opVs2ANWZ1TJoKc937iMmNstM/d0ae1vNbnBvBhqCSezgVUOzcLCqbI5elV8Vy6WKwKjaqR+zO9VKirBBCA==",
"dev": true "dev": true
}, },
"node_modules/@types/gapi": {
"version": "0.0.47",
"resolved": "https://registry.npmjs.org/@types/gapi/-/gapi-0.0.47.tgz",
"integrity": "sha512-/ZsLuq6BffMgbKMtZyDZ8vwQvTyKhKQ1G2K6VyWCgtHHhfSSXbk4+4JwImZiTjWNXfI2q1ZStAwFFHSkNoTkHA=="
},
"node_modules/@types/gapi.client": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/@types/gapi.client/-/gapi.client-1.0.8.tgz",
"integrity": "sha512-qJQUmmumbYym3Amax0S8CVzuSngcXsC1fJdwRS2zeW5lM63zXkw4wJFP+bG0jzgi0R6EsJKoHnGNVTDbOyG1ng==",
"dev": true
},
"node_modules/@types/gapi.client.discovery-v1": {
"version": "0.0.4",
"resolved": "https://registry.npmjs.org/@types/gapi.client.discovery-v1/-/gapi.client.discovery-v1-0.0.4.tgz",
"integrity": "sha512-uevhRumNE65F5mf2gABLaReOmbFSXONuzFZjNR3dYv6BmkHg+wciubHrfBAsp3554zNo3Dcg6dUAlwMqQfpwjQ==",
"dev": true,
"dependencies": {
"@maxim_mazurok/gapi.client.discovery-v1": "latest"
}
},
"node_modules/@types/gapi.client.drive-v3": {
"version": "0.0.4",
"resolved": "https://registry.npmjs.org/@types/gapi.client.drive-v3/-/gapi.client.drive-v3-0.0.4.tgz",
"integrity": "sha512-jE37dJ0EzAdY0aJPFOp20xmec/aO0P4HtUIA9k07RMPyedFDOcuMlSac1r0PklwQdgXF7BHaMoObNHNAnwSQUQ==",
"dev": true,
"dependencies": {
"@maxim_mazurok/gapi.client.drive-v3": "latest"
}
},
"node_modules/@types/google.accounts": {
"version": "0.0.14",
"resolved": "https://registry.npmjs.org/@types/google.accounts/-/google.accounts-0.0.14.tgz",
"integrity": "sha512-HqIVkVzpiLWhlajhQQd4rIV7czanFvXblJI2J1fSrL+VKQuQwwZ63m35D/mI0flsqKE6p/hNrAG0Yn4FD6JvNA=="
},
"node_modules/@types/json-schema": { "node_modules/@types/json-schema": {
"version": "7.0.12", "version": "7.0.12",
"resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.12.tgz", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.12.tgz",
@@ -1257,9 +1314,9 @@
} }
}, },
"node_modules/caniuse-lite": { "node_modules/caniuse-lite": {
"version": "1.0.30001525", "version": "1.0.30001597",
"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001525.tgz", "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001597.tgz",
"integrity": "sha512-/3z+wB4icFt3r0USMwxujAqRvaD/B7rvGTsKhbhSQErVrJvkZCLhgNLJxU8MevahQVH6hCU9FsHdNUFbiwmE7Q==", "integrity": "sha512-7LjJvmQU6Sj7bL0j5b5WY/3n7utXUJvAe1lxhsHDbLmwX9mdL86Yjtr+5SRCyf8qME4M7pU2hswj0FpyBVCv9w==",
"dev": true, "dev": true,
"funding": [ "funding": [
{ {
@@ -4349,6 +4406,26 @@
} }
} }
}, },
"@maxim_mazurok/gapi.client.discovery-v1": {
"version": "0.1.20200806",
"resolved": "https://registry.npmjs.org/@maxim_mazurok/gapi.client.discovery-v1/-/gapi.client.discovery-v1-0.1.20200806.tgz",
"integrity": "sha512-Wl6UfmZVDdWbY3PUu8E2ULk9RPLjnMqp/iOA4tcK8Ne+U/GmlnWP/e34IaZNGArfl7iXJNOG+/3Rj9L9jQyF9Q==",
"dev": true,
"requires": {
"@types/gapi.client": "*",
"@types/gapi.client.discovery-v1": "*"
}
},
"@maxim_mazurok/gapi.client.drive-v3": {
"version": "0.0.20240304",
"resolved": "https://registry.npmjs.org/@maxim_mazurok/gapi.client.drive-v3/-/gapi.client.drive-v3-0.0.20240304.tgz",
"integrity": "sha512-PuGvDvRE70XsyUqnQCq5M2VJ7/E7u5pDvHZfBXunVlf5mYmkyAdbZg+ygJDJ2243wWpUXjmEcuxLDY2yamIq4g==",
"dev": true,
"requires": {
"@types/gapi.client": "*",
"@types/gapi.client.discovery-v1": "*"
}
},
"@nodelib/fs.scandir": { "@nodelib/fs.scandir": {
"version": "2.1.5", "version": "2.1.5",
"resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz",
@@ -4453,6 +4530,40 @@
"integrity": "sha512-LG4opVs2ANWZ1TJoKc937iMmNstM/d0ae1vNbnBvBhqCSezgVUOzcLCqbI5elV8Vy6WKwKjaqR+zO9VKirBBCA==", "integrity": "sha512-LG4opVs2ANWZ1TJoKc937iMmNstM/d0ae1vNbnBvBhqCSezgVUOzcLCqbI5elV8Vy6WKwKjaqR+zO9VKirBBCA==",
"dev": true "dev": true
}, },
"@types/gapi": {
"version": "0.0.47",
"resolved": "https://registry.npmjs.org/@types/gapi/-/gapi-0.0.47.tgz",
"integrity": "sha512-/ZsLuq6BffMgbKMtZyDZ8vwQvTyKhKQ1G2K6VyWCgtHHhfSSXbk4+4JwImZiTjWNXfI2q1ZStAwFFHSkNoTkHA=="
},
"@types/gapi.client": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/@types/gapi.client/-/gapi.client-1.0.8.tgz",
"integrity": "sha512-qJQUmmumbYym3Amax0S8CVzuSngcXsC1fJdwRS2zeW5lM63zXkw4wJFP+bG0jzgi0R6EsJKoHnGNVTDbOyG1ng==",
"dev": true
},
"@types/gapi.client.discovery-v1": {
"version": "0.0.4",
"resolved": "https://registry.npmjs.org/@types/gapi.client.discovery-v1/-/gapi.client.discovery-v1-0.0.4.tgz",
"integrity": "sha512-uevhRumNE65F5mf2gABLaReOmbFSXONuzFZjNR3dYv6BmkHg+wciubHrfBAsp3554zNo3Dcg6dUAlwMqQfpwjQ==",
"dev": true,
"requires": {
"@maxim_mazurok/gapi.client.discovery-v1": "latest"
}
},
"@types/gapi.client.drive-v3": {
"version": "0.0.4",
"resolved": "https://registry.npmjs.org/@types/gapi.client.drive-v3/-/gapi.client.drive-v3-0.0.4.tgz",
"integrity": "sha512-jE37dJ0EzAdY0aJPFOp20xmec/aO0P4HtUIA9k07RMPyedFDOcuMlSac1r0PklwQdgXF7BHaMoObNHNAnwSQUQ==",
"dev": true,
"requires": {
"@maxim_mazurok/gapi.client.drive-v3": "latest"
}
},
"@types/google.accounts": {
"version": "0.0.14",
"resolved": "https://registry.npmjs.org/@types/google.accounts/-/google.accounts-0.0.14.tgz",
"integrity": "sha512-HqIVkVzpiLWhlajhQQd4rIV7czanFvXblJI2J1fSrL+VKQuQwwZ63m35D/mI0flsqKE6p/hNrAG0Yn4FD6JvNA=="
},
"@types/json-schema": { "@types/json-schema": {
"version": "7.0.12", "version": "7.0.12",
"resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.12.tgz", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.12.tgz",
@@ -4785,9 +4896,9 @@
"dev": true "dev": true
}, },
"caniuse-lite": { "caniuse-lite": {
"version": "1.0.30001525", "version": "1.0.30001597",
"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001525.tgz", "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001597.tgz",
"integrity": "sha512-/3z+wB4icFt3r0USMwxujAqRvaD/B7rvGTsKhbhSQErVrJvkZCLhgNLJxU8MevahQVH6hCU9FsHdNUFbiwmE7Q==", "integrity": "sha512-7LjJvmQU6Sj7bL0j5b5WY/3n7utXUJvAe1lxhsHDbLmwX9mdL86Yjtr+5SRCyf8qME4M7pU2hswj0FpyBVCv9w==",
"dev": true "dev": true
}, },
"chalk": { "chalk": {

View File

@@ -14,6 +14,7 @@
"devDependencies": { "devDependencies": {
"@sveltejs/adapter-auto": "^2.0.0", "@sveltejs/adapter-auto": "^2.0.0",
"@sveltejs/kit": "^1.20.4", "@sveltejs/kit": "^1.20.4",
"@types/gapi.client.drive-v3": "^0.0.4",
"@typescript-eslint/eslint-plugin": "^5.45.0", "@typescript-eslint/eslint-plugin": "^5.45.0",
"@typescript-eslint/parser": "^5.45.0", "@typescript-eslint/parser": "^5.45.0",
"autoprefixer": "^10.4.14", "autoprefixer": "^10.4.14",
@@ -36,6 +37,8 @@
}, },
"type": "module", "type": "module",
"dependencies": { "dependencies": {
"@types/gapi": "^0.0.47",
"@types/google.accounts": "^0.0.14",
"@vercel/analytics": "^1.1.0", "@vercel/analytics": "^1.1.0",
"@zip.js/zip.js": "^2.7.20", "@zip.js/zip.js": "^2.7.20",
"dexie": "^4.0.1-alpha.25", "dexie": "^4.0.1-alpha.25",

View File

@@ -3,17 +3,25 @@
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<link rel="icon" href="%sveltekit.assets%/favicon.png" /> <link rel="icon" href="%sveltekit.assets%/favicon.png" />
<link rel="manifest" crossorigin="use-credentials" href="manifest.json"> <link rel="manifest" crossorigin="use-credentials" href="manifest.json" />
<meta <meta
name="viewport" name="viewport"
content="width=device-width, height=device-height,initial-scale=1, minimum-scale=1, user-scalable=no" content="width=device-width, height=device-height,initial-scale=1, minimum-scale=1, user-scalable=no"
/> />
<script src="https://apis.google.com/js/api.js"></script>
<script src="https://accounts.google.com/gsi/client"></script>
%sveltekit.head% %sveltekit.head%
</head> </head>
<body data-sveltekit-preload-data="hover" class="bg-white dark:bg-gray-950 dark:text-white"> <body data-sveltekit-preload-data="hover" class="bg-white dark:bg-gray-950 dark:text-white">
<div <div
id="popupAbout" id="popupAbout"
class="pageContainer" class="pageContainer"
style="display: contents; background-image: url('https://reader.mokuro.app/_app/immutable/assets/icon.06fcfdd6.webp');">%sveltekit.body%</div> style="
display: contents;
background-image: url('https://reader.mokuro.app/_app/immutable/assets/icon.06fcfdd6.webp');
"
>
%sveltekit.body%
</div>
</body> </body>
</html> </html>

View File

@@ -1,7 +1,7 @@
<script lang="ts"> <script lang="ts">
import { Navbar, NavBrand } from 'flowbite-svelte'; import { Navbar, NavBrand } from 'flowbite-svelte';
import { UserSettingsSolid, UploadSolid } from 'flowbite-svelte-icons'; import { UserSettingsSolid, UploadSolid, CloudArrowUpOutline } from 'flowbite-svelte-icons';
import { afterNavigate } from '$app/navigation'; import { afterNavigate, goto } from '$app/navigation';
import { page } from '$app/stores'; import { page } from '$app/stores';
import Settings from './Settings/Settings.svelte'; import Settings from './Settings/Settings.svelte';
import UploadModal from './UploadModal.svelte'; import UploadModal from './UploadModal.svelte';
@@ -37,6 +37,7 @@
<div class="flex md:order-2 gap-5"> <div class="flex md:order-2 gap-5">
<UserSettingsSolid class="hover:text-primary-700" on:click={openSettings} /> <UserSettingsSolid class="hover:text-primary-700" on:click={openSettings} />
<UploadSolid class="hover:text-primary-700" on:click={() => (uploadModalOpen = true)} /> <UploadSolid class="hover:text-primary-700" on:click={() => (uploadModalOpen = true)} />
<CloudArrowUpOutline class="hover:text-primary-700" on:click={() => goto('/cloud')} />
</div> </div>
</Navbar> </Navbar>
</div> </div>

View File

@@ -9,7 +9,6 @@ export * from './web-import'
const zipTypes = ['zip', 'cbz', 'ZIP', 'CBZ']; const zipTypes = ['zip', 'cbz', 'ZIP', 'CBZ'];
const imageTypes = ['image/jpeg', 'image/png', 'image/webp']; const imageTypes = ['image/jpeg', 'image/png', 'image/webp'];
export async function unzipManga(file: File) { export async function unzipManga(file: File) {
const zipFileReader = new Uint8ArrayReader(new Uint8Array(await file.arrayBuffer())); const zipFileReader = new Uint8ArrayReader(new Uint8Array(await file.arrayBuffer()));
const zipReader = new ZipReader(zipFileReader); const zipReader = new ZipReader(zipFileReader);

View File

@@ -0,0 +1,156 @@
<script lang="ts">
import { processFiles } from '$lib/upload';
import Loader from '$lib/components/Loader.svelte';
import { formatBytes, showSnackbar } from '$lib/util';
import { Card } from 'flowbite-svelte';
import { onMount } from 'svelte';
import { promptConfirmation } from '$lib/util';
import { GoogleSolid } from 'flowbite-svelte-icons';
const CLIENT_ID = import.meta.env.VITE_GDRIVE_CLIENT_ID;
const API_KEY = import.meta.env.VITE_GDRIVE_API_KEY;
const DISCOVERY_DOC = 'https://www.googleapis.com/discovery/v1/apis/drive/v3/rest';
const SCOPES = 'https://www.googleapis.com/auth/drive';
const FOLDER_MIME_TYPE = 'application/vnd.google-apps.folder';
let tokenClient: any;
let zips: gapi.client.drive.File[];
let loadingMessage = '';
async function fetchZips(folderId: string) {
const { result } = await gapi.client.drive.files.list({
q: `'${folderId}' in parents and (mimeType='${FOLDER_MIME_TYPE}' or (fileExtension='zip' or fileExtension='cbz'))`,
fields: 'files(id, name, mimeType, size)'
});
if (!result.files) return;
let zipFiles: gapi.client.drive.File[] = [];
for (const file of result.files) {
if (!file.id) continue;
if (file.mimeType === FOLDER_MIME_TYPE) {
zipFiles = [...zipFiles, ...((await fetchZips(file.id)) || [])];
} else {
zipFiles.push(file);
}
}
return zipFiles;
}
async function downloadFile(fileId: string) {
loadingMessage = 'Downloading from drive';
const { body, headers } = await gapi.client.drive.files.get({
fileId,
alt: 'media'
});
const type = headers?.['Content-Type'] || '';
const blob = new Blob([new Uint8Array(body.length).map((_, i) => body.charCodeAt(i))], {
type
});
const file = new File([blob], fileId + '.zip');
loadingMessage = 'Adding to catalog';
await processFiles([file]);
loadingMessage = '';
}
function onClick({ id, name }: gapi.client.drive.File) {
if (id) {
promptConfirmation(`Would you like to download and add ${name} to your collection?`, () => {
downloadFile(id);
});
}
}
async function connectDrive(resp?: any) {
if (resp?.error !== undefined) {
throw resp;
}
loadingMessage = 'Connecting to drive';
const { result } = await gapi.client.drive.files.list({
q: `mimeType='application/vnd.google-apps.folder' and name='MokuroReader'`,
fields: 'files(id)'
});
if (result.files?.length === 0) {
await gapi.client.drive.files.create({
resource: { mimeType: FOLDER_MIME_TYPE, name: 'MokuroReader' },
fields: 'id'
});
} else {
zips = [...((await fetchZips(result.files?.[0]?.id || '')) || [])];
loadingMessage = '';
}
showSnackbar('Connected to Google Drive');
}
function signIn() {
if (gapi.client.getToken() === null) {
tokenClient.requestAccessToken({ prompt: 'consent' });
} else {
tokenClient.requestAccessToken({ prompt: '' });
}
}
onMount(() => {
gapi.load('client', async () => {
await gapi.client.init({
apiKey: API_KEY,
discoveryDocs: [DISCOVERY_DOC]
});
if (gapi.client.getToken() !== null) {
loadingMessage = 'Connecting to drive';
connectDrive();
}
});
tokenClient = google.accounts.oauth2.initTokenClient({
client_id: CLIENT_ID,
scope: SCOPES,
callback: connectDrive
});
});
</script>
<div class="p-10 h-[90svh]">
{#if loadingMessage}
<Loader>
{loadingMessage}
</Loader>
{:else if zips}
<div class="flex flex-col gap-5">
<h2 class="text-lg font-semibold text-center">Google drive:</h2>
<div class="flex gap-2 flex-wrap p-10 border rounded-lg border-slate-600 border-opacity-50">
{#each zips as zip}
<Card href="#" on:click={() => onClick(zip)}>
<h2 class="font-semibold">{zip.name}</h2>
<p>{formatBytes(parseInt(zip.size || '0'))}</p>
</Card>
{/each}
</div>
</div>
{:else}
<button
class="w-full border rounded-lg border-slate-600 p-10 border-opacity-50 hover:bg-slate-800"
on:click={signIn}
>
<div class="flex gap-2 items-center justify-center">
<GoogleSolid size="lg" />
<h2 class="text-lg">Connect to Google Drive</h2>
</div>
</button>
{/if}
</div>