-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.ts
More file actions
94 lines (73 loc) · 2.73 KB
/
examples.ts
File metadata and controls
94 lines (73 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env node
/* eslint-disable no-console,@typescript-eslint/no-unused-vars,import/no-extraneous-dependencies */
import readline from 'node:readline';
import { createWriteStream } from 'node:fs';
import { pipeline } from 'node:stream/promises';
import { execa } from 'execa';
import { writeFile, readFile } from 'node:fs/promises';
import makeApi, { State } from './src/api.js';
const stateFileName = 'jotta-state.json';
async function readState() {
try {
return JSON.parse(await readFile(stateFileName, 'utf8'));
} catch (err) {
if (!(err instanceof Error && 'code' in err && err.code === 'ENOENT')) {
console.error('Could not read state from', stateFileName, err);
}
return {};
}
}
const writeState = async (newState: State) => writeFile(stateFileName, JSON.stringify(newState, null, 2));
// Load initial from disk
const initialState = await readState();
// Create the API client
const api = makeApi({
initialState,
onStateUpdated: async (newState) => {
console.log('State updated', newState);
await writeState(newState);
},
onAuthFailed: () => {
console.warn('Please login again');
},
});
async function login() {
function ask(question: string): Promise<string> {
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
return new Promise((resolve) => rl.question(question, (ans) => { rl.close(); resolve(ans.trim()); }));
}
const loginTokenBase64 = await ask('Go to https://jottacloud.com/web/secure and create a Jottacloud personal login. Paste the token here:');
return api.authenticate(loginTokenBase64);
}
// Login first, if we don't have a token yet
if (initialState.token == null) {
console.log('Logging in');
await login();
}
// Get customer info
// console.log(await api.getCustomerInfo());
// List root
console.log(await api.listFiles());
// List "photos" folder
// console.log(await api.listFiles('photos'));
// List all files recursively
// await api.listFilesRecursive('photos', ({ path, files }) => console.log(path, files.length, 'files'));
// Calling listFiles on a file returns the file info
// console.log(await api.listFiles('photos/image.jpeg'));
// Download a file
/*
await pipeline(
(await api.getFile('photos/video.mp4', { mode: 'bin' })).body!,
createWriteStream('video.mp4'),
);
*/
// Download a file's large (WL) thumbnail
/*
await pipeline(
(await api.getFile('photos/video.mp4', { mode: 'thumb', ts: 'WL' })).body!,
createWriteStream('video-thumb.jpeg'),
);
*/
// Share a file (makes it publicly accessible). It also returns the public download URL
// All API calls for this file will also include the publicDownloadUrl property after sharing it.
// console.log((await api.shareFile('photos/video.mp4', true)).publicDownloadUrl);