@rsweeten/dropbox-sync 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +315 -315
- package/package.json +74 -66
- package/src/core/sync.ts +476 -476
package/README.md
CHANGED
@@ -1,315 +1,315 @@
|
|
1
|
-
# Dropbox Sync Module
|
2
|
-
|
3
|
-
A reusable TypeScript module for syncing files between your application and Dropbox. Features framework-specific adapters for Next.js, SvelteKit, Nuxt, and Angular.
|
4
|
-
|
5
|
-
> N.B. This NPM package is neither tested nor supported
|
6
|
-
|
7
|
-
## Features
|
8
|
-
|
9
|
-
- **File Synchronization**: Upload local files to Dropbox and download Dropbox files to your local filesystem
|
10
|
-
- **Real-time Progress**: Integrated Socket.IO support for real-time sync progress updates
|
11
|
-
- **Path Normalization**: Robust path handling to ensure consistent comparison between local and remote paths
|
12
|
-
- **OAuth Authentication**: Built-in OAuth flow handling for Dropbox API authentication
|
13
|
-
- **Framework Adapters**: Ready-to-use integrations for Next.js, SvelteKit, and Angular
|
14
|
-
- **TypeScript Support**: Fully typed API for improved developer experience
|
15
|
-
|
16
|
-
## Installation
|
17
|
-
|
18
|
-
```bash
|
19
|
-
npm install dropbox-sync socket.io socket.io-client
|
20
|
-
```
|
21
|
-
|
22
|
-
## Quick Start
|
23
|
-
|
24
|
-
### Core Usage
|
25
|
-
|
26
|
-
```typescript
|
27
|
-
import createDropboxSyncClient from 'dropbox-sync'
|
28
|
-
|
29
|
-
// Create a Dropbox sync client
|
30
|
-
const dropboxSync = createDropboxSyncClient({
|
31
|
-
clientId: 'YOUR_DROPBOX_APP_KEY',
|
32
|
-
clientSecret: 'YOUR_DROPBOX_APP_SECRET',
|
33
|
-
accessToken: 'OPTIONAL_EXISTING_TOKEN',
|
34
|
-
refreshToken: 'OPTIONAL_REFRESH_TOKEN',
|
35
|
-
})
|
36
|
-
|
37
|
-
// Start OAuth flow
|
38
|
-
const authUrl = await dropboxSync.auth.getAuthUrl(
|
39
|
-
'http://localhost:3000/callback'
|
40
|
-
)
|
41
|
-
// Redirect the user to authUrl
|
42
|
-
|
43
|
-
// Exchange authorization code for tokens
|
44
|
-
const tokens = await dropboxSync.auth.exchangeCodeForToken(code, redirectUri)
|
45
|
-
// Save tokens.accessToken and tokens.refreshToken
|
46
|
-
|
47
|
-
// Sync files
|
48
|
-
const syncResult = await dropboxSync.sync.syncFiles({
|
49
|
-
localDir: './public/img',
|
50
|
-
dropboxDir: '/app-images',
|
51
|
-
})
|
52
|
-
|
53
|
-
console.log(`Uploaded ${syncResult.uploaded.length} files`)
|
54
|
-
console.log(`Downloaded ${syncResult.downloaded.length} files`)
|
55
|
-
```
|
56
|
-
|
57
|
-
## Framework-Specific Usage
|
58
|
-
|
59
|
-
### Next.js
|
60
|
-
|
61
|
-
```typescript
|
62
|
-
// In your component
|
63
|
-
import { useNextDropboxSync } from 'dropbox-sync'
|
64
|
-
|
65
|
-
export function DropboxComponent() {
|
66
|
-
// Initialize client
|
67
|
-
const dropboxSync = useNextDropboxSync({
|
68
|
-
clientId: process.env.NEXT_PUBLIC_DROPBOX_APP_KEY,
|
69
|
-
})
|
70
|
-
|
71
|
-
// Connect to socket for real-time updates
|
72
|
-
useEffect(() => {
|
73
|
-
dropboxSync.socket.connect()
|
74
|
-
dropboxSync.socket.on('sync:progress', handleProgress)
|
75
|
-
|
76
|
-
return () => {
|
77
|
-
dropboxSync.socket.disconnect()
|
78
|
-
}
|
79
|
-
}, [])
|
80
|
-
|
81
|
-
// Start sync
|
82
|
-
const handleSync = () => {
|
83
|
-
// This will trigger the server-side sync through Socket.IO
|
84
|
-
dropboxSync.socket.emit('dropbox:sync')
|
85
|
-
}
|
86
|
-
|
87
|
-
return <button onClick={handleSync}>Sync with Dropbox</button>
|
88
|
-
}
|
89
|
-
|
90
|
-
// In your API route
|
91
|
-
import { createNextDropboxApiHandlers } from 'dropbox-sync'
|
92
|
-
|
93
|
-
const handlers = createNextDropboxApiHandlers()
|
94
|
-
|
95
|
-
export async function GET(request) {
|
96
|
-
return handlers.status()
|
97
|
-
}
|
98
|
-
```
|
99
|
-
|
100
|
-
### Nuxt.js
|
101
|
-
|
102
|
-
The Nuxt adapter leverages Nuxt's runtime config system for app settings and uses H3 (Nitro's HTTP server) utilities for handling requests and cookies. It's designed to work seamlessly with Nuxt 3's composition API and server routes.
|
103
|
-
|
104
|
-
To use this adapter in a Nuxt project, developers would:
|
105
|
-
|
106
|
-
1. Add runtime config in their nuxt.config.ts:
|
107
|
-
|
108
|
-
```typescript
|
109
|
-
export default defineNuxtConfig({
|
110
|
-
runtimeConfig: {
|
111
|
-
// Private keys
|
112
|
-
dropboxAppSecret: process.env.DROPBOX_APP_SECRET,
|
113
|
-
dropboxRedirectUri: process.env.DROPBOX_REDIRECT_URI,
|
114
|
-
|
115
|
-
// Public keys
|
116
|
-
public: {
|
117
|
-
dropboxAppKey: process.env.DROPBOX_APP_KEY,
|
118
|
-
appUrl: process.env.APP_URL || 'http://localhost:3000',
|
119
|
-
},
|
120
|
-
},
|
121
|
-
})
|
122
|
-
```
|
123
|
-
|
124
|
-
2. Create a plugin using the example provided
|
125
|
-
3. Set up API routes using the handlers from `createNuxtApiHandlers()`
|
126
|
-
|
127
|
-
```typescript
|
128
|
-
// In your plugins/dropbox.ts
|
129
|
-
import { useNuxtDropboxSync } from 'dropbox-sync'
|
130
|
-
import { defineNuxtPlugin } from 'nuxt/app'
|
131
|
-
|
132
|
-
export default defineNuxtPlugin((nuxtApp) => {
|
133
|
-
// Create the Dropbox client
|
134
|
-
const dropboxSync = useNuxtDropboxSync()
|
135
|
-
|
136
|
-
// Provide the client to the app
|
137
|
-
return {
|
138
|
-
provide: {
|
139
|
-
dropbox: dropboxSync
|
140
|
-
}
|
141
|
-
}
|
142
|
-
})
|
143
|
-
|
144
|
-
// In your Vue component
|
145
|
-
<script setup>
|
146
|
-
import { ref, onMounted, onBeforeUnmount } from 'vue'
|
147
|
-
|
148
|
-
const { $dropbox } = useNuxtApp()
|
149
|
-
const syncProgress = ref(0)
|
150
|
-
|
151
|
-
// Handle progress updates
|
152
|
-
const handleProgress = (data) => {
|
153
|
-
syncProgress.value = data.progress
|
154
|
-
}
|
155
|
-
|
156
|
-
// Connect to socket
|
157
|
-
onMounted(() => {
|
158
|
-
$dropbox.socket.connect()
|
159
|
-
$dropbox.socket.on('sync:progress', handleProgress)
|
160
|
-
})
|
161
|
-
|
162
|
-
// Clean up on unmount
|
163
|
-
onBeforeUnmount(() => {
|
164
|
-
$dropbox.socket.disconnect()
|
165
|
-
})
|
166
|
-
|
167
|
-
// Start sync
|
168
|
-
const startSync = () => {
|
169
|
-
$dropbox.socket.emit('dropbox:sync')
|
170
|
-
}
|
171
|
-
</script>
|
172
|
-
|
173
|
-
<template>
|
174
|
-
<div>
|
175
|
-
<button @click="startSync">Sync with Dropbox</button>
|
176
|
-
<progress :value="syncProgress" max="100"></progress>
|
177
|
-
</div>
|
178
|
-
</template>
|
179
|
-
|
180
|
-
// In your server/api/dropbox/[...].ts
|
181
|
-
import { createNuxtApiHandlers } from 'dropbox-sync'
|
182
|
-
import { defineEventHandler } from 'h3'
|
183
|
-
|
184
|
-
// Create handlers
|
185
|
-
const handlers = createNuxtApiHandlers()
|
186
|
-
|
187
|
-
export default defineEventHandler(async (event) => {
|
188
|
-
// Handle different endpoints
|
189
|
-
const path = event.path || ''
|
190
|
-
|
191
|
-
if (path.endsWith('/status')) {
|
192
|
-
return await handlers.status(event)
|
193
|
-
}
|
194
|
-
|
195
|
-
if (path.endsWith('/auth')) {
|
196
|
-
return await handlers.oauthStart(event)
|
197
|
-
}
|
198
|
-
|
199
|
-
if (path.endsWith('/auth/callback')) {
|
200
|
-
return await handlers.oauthCallback(event)
|
201
|
-
}
|
202
|
-
|
203
|
-
if (path.endsWith('/logout')) {
|
204
|
-
return await handlers.logout(event)
|
205
|
-
}
|
206
|
-
|
207
|
-
return { error: 'Not found' }
|
208
|
-
})
|
209
|
-
```
|
210
|
-
|
211
|
-
### SvelteKit
|
212
|
-
|
213
|
-
```typescript
|
214
|
-
// In your store
|
215
|
-
import { useSvelteDropboxSync } from 'dropbox-sync'
|
216
|
-
import { writable } from 'svelte/store'
|
217
|
-
|
218
|
-
// Create store
|
219
|
-
export function createDropboxStore() {
|
220
|
-
const dropboxSync = useSvelteDropboxSync({
|
221
|
-
clientId: import.meta.env.VITE_DROPBOX_APP_KEY,
|
222
|
-
})
|
223
|
-
|
224
|
-
const progress = writable(0)
|
225
|
-
|
226
|
-
// Set up socket listeners
|
227
|
-
dropboxSync.socket.connect()
|
228
|
-
dropboxSync.socket.on('sync:progress', (data) => {
|
229
|
-
progress.set(data.progress)
|
230
|
-
})
|
231
|
-
|
232
|
-
return {
|
233
|
-
progress: { subscribe: progress.subscribe },
|
234
|
-
startSync: () => {
|
235
|
-
dropboxSync.socket.emit('dropbox:sync')
|
236
|
-
},
|
237
|
-
}
|
238
|
-
}
|
239
|
-
```
|
240
|
-
|
241
|
-
### Angular
|
242
|
-
|
243
|
-
```typescript
|
244
|
-
// In your service
|
245
|
-
import { DropboxSyncService, getCredentialsFromEnvironment } from 'dropbox-sync'
|
246
|
-
import { environment } from '../environments/environment'
|
247
|
-
|
248
|
-
@Injectable({ providedIn: 'root' })
|
249
|
-
export class DropboxService {
|
250
|
-
constructor(private dropboxSyncService: DropboxSyncService) {
|
251
|
-
// Initialize the service
|
252
|
-
this.dropboxSyncService.initialize(
|
253
|
-
getCredentialsFromEnvironment(environment)
|
254
|
-
)
|
255
|
-
|
256
|
-
// Listen for socket events
|
257
|
-
const socketEvents = this.dropboxSyncService.setupSocketListeners()
|
258
|
-
socketEvents.subscribe((event) => console.log(event))
|
259
|
-
}
|
260
|
-
|
261
|
-
startSync() {
|
262
|
-
return this.dropboxSyncService.startSync()
|
263
|
-
}
|
264
|
-
}
|
265
|
-
```
|
266
|
-
|
267
|
-
## API Reference
|
268
|
-
|
269
|
-
### Core Client
|
270
|
-
|
271
|
-
- `createDropboxSyncClient(credentials)` - Creates a new client instance
|
272
|
-
|
273
|
-
### Auth Methods
|
274
|
-
|
275
|
-
- `getAuthUrl(redirectUri, state?)` - Generate an OAuth authentication URL
|
276
|
-
- `exchangeCodeForToken(code, redirectUri)` - Exchange authorization code for access token
|
277
|
-
- `refreshAccessToken()` - Refresh an expired access token
|
278
|
-
|
279
|
-
### Sync Methods
|
280
|
-
|
281
|
-
- `scanLocalFiles(dir?)` - Scan local directory for files
|
282
|
-
- `scanDropboxFiles(dir?)` - Scan Dropbox folder for files
|
283
|
-
- `createSyncQueue(options?)` - Create upload/download queues
|
284
|
-
- `syncFiles(options?)` - Synchronize files between local and Dropbox
|
285
|
-
- `cancelSync()` - Cancel an ongoing synchronization
|
286
|
-
|
287
|
-
### Socket Methods
|
288
|
-
|
289
|
-
- `connect()` - Connect to Socket.IO
|
290
|
-
- `disconnect()` - Disconnect from Socket.IO
|
291
|
-
- `on(event, handler)` - Listen for an event
|
292
|
-
- `off(event)` - Remove event listener
|
293
|
-
- `emit(event, ...args)` - Emit an event
|
294
|
-
|
295
|
-
## Socket Events
|
296
|
-
|
297
|
-
- `sync:progress` - Emitted during sync with progress information
|
298
|
-
- `sync:queue` - Emitted at the start of sync with queue information
|
299
|
-
- `sync:complete` - Emitted when sync is complete
|
300
|
-
- `sync:error` - Emitted when an error occurs during sync
|
301
|
-
|
302
|
-
## Configuration Options
|
303
|
-
|
304
|
-
```typescript
|
305
|
-
interface SyncOptions {
|
306
|
-
localDir: string // Local directory path
|
307
|
-
dropboxDir?: string // Dropbox folder path
|
308
|
-
fileTypes?: RegExp // File types to sync (default: images and JSON)
|
309
|
-
progressCallback?: (progress: SyncProgress) => void
|
310
|
-
}
|
311
|
-
```
|
312
|
-
|
313
|
-
## License
|
314
|
-
|
315
|
-
MIT
|
1
|
+
# Dropbox Sync Module
|
2
|
+
|
3
|
+
A reusable TypeScript module for syncing files between your application and Dropbox. Features framework-specific adapters for Next.js, SvelteKit, Nuxt, and Angular.
|
4
|
+
|
5
|
+
> N.B. This NPM package is neither tested nor supported
|
6
|
+
|
7
|
+
## Features
|
8
|
+
|
9
|
+
- **File Synchronization**: Upload local files to Dropbox and download Dropbox files to your local filesystem
|
10
|
+
- **Real-time Progress**: Integrated Socket.IO support for real-time sync progress updates
|
11
|
+
- **Path Normalization**: Robust path handling to ensure consistent comparison between local and remote paths
|
12
|
+
- **OAuth Authentication**: Built-in OAuth flow handling for Dropbox API authentication
|
13
|
+
- **Framework Adapters**: Ready-to-use integrations for Next.js, SvelteKit, and Angular
|
14
|
+
- **TypeScript Support**: Fully typed API for improved developer experience
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
```bash
|
19
|
+
npm install dropbox-sync socket.io socket.io-client
|
20
|
+
```
|
21
|
+
|
22
|
+
## Quick Start
|
23
|
+
|
24
|
+
### Core Usage
|
25
|
+
|
26
|
+
```typescript
|
27
|
+
import createDropboxSyncClient from 'dropbox-sync'
|
28
|
+
|
29
|
+
// Create a Dropbox sync client
|
30
|
+
const dropboxSync = createDropboxSyncClient({
|
31
|
+
clientId: 'YOUR_DROPBOX_APP_KEY',
|
32
|
+
clientSecret: 'YOUR_DROPBOX_APP_SECRET',
|
33
|
+
accessToken: 'OPTIONAL_EXISTING_TOKEN',
|
34
|
+
refreshToken: 'OPTIONAL_REFRESH_TOKEN',
|
35
|
+
})
|
36
|
+
|
37
|
+
// Start OAuth flow
|
38
|
+
const authUrl = await dropboxSync.auth.getAuthUrl(
|
39
|
+
'http://localhost:3000/callback'
|
40
|
+
)
|
41
|
+
// Redirect the user to authUrl
|
42
|
+
|
43
|
+
// Exchange authorization code for tokens
|
44
|
+
const tokens = await dropboxSync.auth.exchangeCodeForToken(code, redirectUri)
|
45
|
+
// Save tokens.accessToken and tokens.refreshToken
|
46
|
+
|
47
|
+
// Sync files
|
48
|
+
const syncResult = await dropboxSync.sync.syncFiles({
|
49
|
+
localDir: './public/img',
|
50
|
+
dropboxDir: '/app-images',
|
51
|
+
})
|
52
|
+
|
53
|
+
console.log(`Uploaded ${syncResult.uploaded.length} files`)
|
54
|
+
console.log(`Downloaded ${syncResult.downloaded.length} files`)
|
55
|
+
```
|
56
|
+
|
57
|
+
## Framework-Specific Usage
|
58
|
+
|
59
|
+
### Next.js
|
60
|
+
|
61
|
+
```typescript
|
62
|
+
// In your component
|
63
|
+
import { useNextDropboxSync } from 'dropbox-sync'
|
64
|
+
|
65
|
+
export function DropboxComponent() {
|
66
|
+
// Initialize client
|
67
|
+
const dropboxSync = useNextDropboxSync({
|
68
|
+
clientId: process.env.NEXT_PUBLIC_DROPBOX_APP_KEY,
|
69
|
+
})
|
70
|
+
|
71
|
+
// Connect to socket for real-time updates
|
72
|
+
useEffect(() => {
|
73
|
+
dropboxSync.socket.connect()
|
74
|
+
dropboxSync.socket.on('sync:progress', handleProgress)
|
75
|
+
|
76
|
+
return () => {
|
77
|
+
dropboxSync.socket.disconnect()
|
78
|
+
}
|
79
|
+
}, [])
|
80
|
+
|
81
|
+
// Start sync
|
82
|
+
const handleSync = () => {
|
83
|
+
// This will trigger the server-side sync through Socket.IO
|
84
|
+
dropboxSync.socket.emit('dropbox:sync')
|
85
|
+
}
|
86
|
+
|
87
|
+
return <button onClick={handleSync}>Sync with Dropbox</button>
|
88
|
+
}
|
89
|
+
|
90
|
+
// In your API route
|
91
|
+
import { createNextDropboxApiHandlers } from 'dropbox-sync'
|
92
|
+
|
93
|
+
const handlers = createNextDropboxApiHandlers()
|
94
|
+
|
95
|
+
export async function GET(request) {
|
96
|
+
return handlers.status()
|
97
|
+
}
|
98
|
+
```
|
99
|
+
|
100
|
+
### Nuxt.js
|
101
|
+
|
102
|
+
The Nuxt adapter leverages Nuxt's runtime config system for app settings and uses H3 (Nitro's HTTP server) utilities for handling requests and cookies. It's designed to work seamlessly with Nuxt 3's composition API and server routes.
|
103
|
+
|
104
|
+
To use this adapter in a Nuxt project, developers would:
|
105
|
+
|
106
|
+
1. Add runtime config in their nuxt.config.ts:
|
107
|
+
|
108
|
+
```typescript
|
109
|
+
export default defineNuxtConfig({
|
110
|
+
runtimeConfig: {
|
111
|
+
// Private keys
|
112
|
+
dropboxAppSecret: process.env.DROPBOX_APP_SECRET,
|
113
|
+
dropboxRedirectUri: process.env.DROPBOX_REDIRECT_URI,
|
114
|
+
|
115
|
+
// Public keys
|
116
|
+
public: {
|
117
|
+
dropboxAppKey: process.env.DROPBOX_APP_KEY,
|
118
|
+
appUrl: process.env.APP_URL || 'http://localhost:3000',
|
119
|
+
},
|
120
|
+
},
|
121
|
+
})
|
122
|
+
```
|
123
|
+
|
124
|
+
2. Create a plugin using the example provided
|
125
|
+
3. Set up API routes using the handlers from `createNuxtApiHandlers()`
|
126
|
+
|
127
|
+
```typescript
|
128
|
+
// In your plugins/dropbox.ts
|
129
|
+
import { useNuxtDropboxSync } from 'dropbox-sync'
|
130
|
+
import { defineNuxtPlugin } from 'nuxt/app'
|
131
|
+
|
132
|
+
export default defineNuxtPlugin((nuxtApp) => {
|
133
|
+
// Create the Dropbox client
|
134
|
+
const dropboxSync = useNuxtDropboxSync()
|
135
|
+
|
136
|
+
// Provide the client to the app
|
137
|
+
return {
|
138
|
+
provide: {
|
139
|
+
dropbox: dropboxSync
|
140
|
+
}
|
141
|
+
}
|
142
|
+
})
|
143
|
+
|
144
|
+
// In your Vue component
|
145
|
+
<script setup>
|
146
|
+
import { ref, onMounted, onBeforeUnmount } from 'vue'
|
147
|
+
|
148
|
+
const { $dropbox } = useNuxtApp()
|
149
|
+
const syncProgress = ref(0)
|
150
|
+
|
151
|
+
// Handle progress updates
|
152
|
+
const handleProgress = (data) => {
|
153
|
+
syncProgress.value = data.progress
|
154
|
+
}
|
155
|
+
|
156
|
+
// Connect to socket
|
157
|
+
onMounted(() => {
|
158
|
+
$dropbox.socket.connect()
|
159
|
+
$dropbox.socket.on('sync:progress', handleProgress)
|
160
|
+
})
|
161
|
+
|
162
|
+
// Clean up on unmount
|
163
|
+
onBeforeUnmount(() => {
|
164
|
+
$dropbox.socket.disconnect()
|
165
|
+
})
|
166
|
+
|
167
|
+
// Start sync
|
168
|
+
const startSync = () => {
|
169
|
+
$dropbox.socket.emit('dropbox:sync')
|
170
|
+
}
|
171
|
+
</script>
|
172
|
+
|
173
|
+
<template>
|
174
|
+
<div>
|
175
|
+
<button @click="startSync">Sync with Dropbox</button>
|
176
|
+
<progress :value="syncProgress" max="100"></progress>
|
177
|
+
</div>
|
178
|
+
</template>
|
179
|
+
|
180
|
+
// In your server/api/dropbox/[...].ts
|
181
|
+
import { createNuxtApiHandlers } from 'dropbox-sync'
|
182
|
+
import { defineEventHandler } from 'h3'
|
183
|
+
|
184
|
+
// Create handlers
|
185
|
+
const handlers = createNuxtApiHandlers()
|
186
|
+
|
187
|
+
export default defineEventHandler(async (event) => {
|
188
|
+
// Handle different endpoints
|
189
|
+
const path = event.path || ''
|
190
|
+
|
191
|
+
if (path.endsWith('/status')) {
|
192
|
+
return await handlers.status(event)
|
193
|
+
}
|
194
|
+
|
195
|
+
if (path.endsWith('/auth')) {
|
196
|
+
return await handlers.oauthStart(event)
|
197
|
+
}
|
198
|
+
|
199
|
+
if (path.endsWith('/auth/callback')) {
|
200
|
+
return await handlers.oauthCallback(event)
|
201
|
+
}
|
202
|
+
|
203
|
+
if (path.endsWith('/logout')) {
|
204
|
+
return await handlers.logout(event)
|
205
|
+
}
|
206
|
+
|
207
|
+
return { error: 'Not found' }
|
208
|
+
})
|
209
|
+
```
|
210
|
+
|
211
|
+
### SvelteKit
|
212
|
+
|
213
|
+
```typescript
|
214
|
+
// In your store
|
215
|
+
import { useSvelteDropboxSync } from 'dropbox-sync'
|
216
|
+
import { writable } from 'svelte/store'
|
217
|
+
|
218
|
+
// Create store
|
219
|
+
export function createDropboxStore() {
|
220
|
+
const dropboxSync = useSvelteDropboxSync({
|
221
|
+
clientId: import.meta.env.VITE_DROPBOX_APP_KEY,
|
222
|
+
})
|
223
|
+
|
224
|
+
const progress = writable(0)
|
225
|
+
|
226
|
+
// Set up socket listeners
|
227
|
+
dropboxSync.socket.connect()
|
228
|
+
dropboxSync.socket.on('sync:progress', (data) => {
|
229
|
+
progress.set(data.progress)
|
230
|
+
})
|
231
|
+
|
232
|
+
return {
|
233
|
+
progress: { subscribe: progress.subscribe },
|
234
|
+
startSync: () => {
|
235
|
+
dropboxSync.socket.emit('dropbox:sync')
|
236
|
+
},
|
237
|
+
}
|
238
|
+
}
|
239
|
+
```
|
240
|
+
|
241
|
+
### Angular
|
242
|
+
|
243
|
+
```typescript
|
244
|
+
// In your service
|
245
|
+
import { DropboxSyncService, getCredentialsFromEnvironment } from 'dropbox-sync'
|
246
|
+
import { environment } from '../environments/environment'
|
247
|
+
|
248
|
+
@Injectable({ providedIn: 'root' })
|
249
|
+
export class DropboxService {
|
250
|
+
constructor(private dropboxSyncService: DropboxSyncService) {
|
251
|
+
// Initialize the service
|
252
|
+
this.dropboxSyncService.initialize(
|
253
|
+
getCredentialsFromEnvironment(environment)
|
254
|
+
)
|
255
|
+
|
256
|
+
// Listen for socket events
|
257
|
+
const socketEvents = this.dropboxSyncService.setupSocketListeners()
|
258
|
+
socketEvents.subscribe((event) => console.log(event))
|
259
|
+
}
|
260
|
+
|
261
|
+
startSync() {
|
262
|
+
return this.dropboxSyncService.startSync()
|
263
|
+
}
|
264
|
+
}
|
265
|
+
```
|
266
|
+
|
267
|
+
## API Reference
|
268
|
+
|
269
|
+
### Core Client
|
270
|
+
|
271
|
+
- `createDropboxSyncClient(credentials)` - Creates a new client instance
|
272
|
+
|
273
|
+
### Auth Methods
|
274
|
+
|
275
|
+
- `getAuthUrl(redirectUri, state?)` - Generate an OAuth authentication URL
|
276
|
+
- `exchangeCodeForToken(code, redirectUri)` - Exchange authorization code for access token
|
277
|
+
- `refreshAccessToken()` - Refresh an expired access token
|
278
|
+
|
279
|
+
### Sync Methods
|
280
|
+
|
281
|
+
- `scanLocalFiles(dir?)` - Scan local directory for files
|
282
|
+
- `scanDropboxFiles(dir?)` - Scan Dropbox folder for files
|
283
|
+
- `createSyncQueue(options?)` - Create upload/download queues
|
284
|
+
- `syncFiles(options?)` - Synchronize files between local and Dropbox
|
285
|
+
- `cancelSync()` - Cancel an ongoing synchronization
|
286
|
+
|
287
|
+
### Socket Methods
|
288
|
+
|
289
|
+
- `connect()` - Connect to Socket.IO
|
290
|
+
- `disconnect()` - Disconnect from Socket.IO
|
291
|
+
- `on(event, handler)` - Listen for an event
|
292
|
+
- `off(event)` - Remove event listener
|
293
|
+
- `emit(event, ...args)` - Emit an event
|
294
|
+
|
295
|
+
## Socket Events
|
296
|
+
|
297
|
+
- `sync:progress` - Emitted during sync with progress information
|
298
|
+
- `sync:queue` - Emitted at the start of sync with queue information
|
299
|
+
- `sync:complete` - Emitted when sync is complete
|
300
|
+
- `sync:error` - Emitted when an error occurs during sync
|
301
|
+
|
302
|
+
## Configuration Options
|
303
|
+
|
304
|
+
```typescript
|
305
|
+
interface SyncOptions {
|
306
|
+
localDir: string // Local directory path
|
307
|
+
dropboxDir?: string // Dropbox folder path
|
308
|
+
fileTypes?: RegExp // File types to sync (default: images and JSON)
|
309
|
+
progressCallback?: (progress: SyncProgress) => void
|
310
|
+
}
|
311
|
+
```
|
312
|
+
|
313
|
+
## License
|
314
|
+
|
315
|
+
MIT
|