File size: 6,380 Bytes
4e1096a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { md5 } from 'js-md5';
import { Book } from '@/types/book';
import { KOSyncSettings } from '@/types/settings';
import { fetch as tauriFetch } from '@tauri-apps/plugin-http';
import { KoSyncProxyPayload } from '@/types/kosync';
import { isLanAddress } from '@/utils/network';
import { getAPIBaseUrl, isTauriAppPlatform } from '../environment';

/**
 * Interface for KOSync progress response from the server
 */
export interface KoSyncProgress {
  document?: string;
  progress?: string;
  percentage?: number;
  timestamp?: number;
  device?: string;
  device_id?: string;
}

export class KOSyncClient {
  private config: KOSyncSettings;
  private isLanServer: boolean;

  constructor(config: KOSyncSettings) {
    this.config = config;
    this.config.serverUrl = config.serverUrl.replace(/\/$/, '');
    this.isLanServer = isLanAddress(this.config.serverUrl);
  }

  private async request(
    endpoint: string,
    options: {
      method?: 'GET' | 'POST' | 'PUT';
      body?: BodyInit | null;
      headers?: HeadersInit;
      useAuth?: boolean;
    } = {},
  ): Promise<Response> {
    const { method = 'GET', body, headers: additionalHeaders, useAuth = true } = options;

    const headers = new Headers(additionalHeaders || {});
    if (useAuth) {
      headers.set('X-Auth-User', this.config.username);
      headers.set('X-Auth-Key', this.config.userkey);
    }

    if (this.isLanServer || isTauriAppPlatform()) {
      const fetch = isTauriAppPlatform() ? tauriFetch : window.fetch;
      const directUrl = `${this.config.serverUrl}${endpoint}`;

      return fetch(directUrl, {
        method,
        headers: {
          accept: 'application/vnd.koreader.v1+json',
          ...(method === 'GET' ? {} : { 'Content-Type': 'application/json' }),
          ...Object.fromEntries(headers.entries()),
        },
        body,
        danger: {
          acceptInvalidCerts: true,
          acceptInvalidHostnames: true,
        },
      });
    }

    const proxyUrl = `${getAPIBaseUrl()}/kosync`;

    const proxyBody: KoSyncProxyPayload = {
      serverUrl: this.config.serverUrl,
      endpoint,
      method,
      headers: Object.fromEntries(headers.entries()),
      body: body ? JSON.parse(body as string) : undefined,
    };

    return fetch(proxyUrl, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(proxyBody),
    });
  }

  /**
   * Connects to the KOSync server with authentication
   * @param username - The username for authentication
   * @param password - The password for authentication
   * @returns Promise with success status and optional message
   */
  async connect(
    username: string,
    password: string,
  ): Promise<{ success: boolean; message?: string }> {
    const userkey = md5(password);

    try {
      const authResponse = await this.request('/users/auth', {
        method: 'GET',
        headers: {
          'X-Auth-User': username,
          'X-Auth-Key': userkey,
        },
      });

      if (authResponse.ok) {
        return { success: true, message: 'Login successful.' };
      }

      if (authResponse.status === 401) {
        const registerResponse = await this.request('/users/create', {
          method: 'POST',
          useAuth: false,
          body: JSON.stringify({ username, password: userkey }),
        });

        if (registerResponse.ok) {
          return { success: true, message: 'Registration successful.' };
        }

        const regError = await registerResponse.json().catch(() => ({}));
        if (registerResponse.status === 402) {
          return { success: false, message: 'Invalid credentials.' };
        }
        return { success: false, message: regError.message || 'Registration failed.' };
      }

      const errorBody = await authResponse.json().catch(() => ({}));
      return {
        success: false,
        message: errorBody.message || `Authorization failed with status: ${authResponse.status}`,
      };
    } catch (e) {
      console.error('KOSync connection failed', e);
      return { success: false, message: (e as Error).message || 'Connection error.' };
    }
  }

  /**
   * Retrieves the reading progress for a specific book from the server
   * @param book - The book to get progress for
   * @returns Promise with the progress data or null if not found
   */
  async getProgress(book: Book): Promise<KoSyncProgress | null> {
    if (!this.config.userkey) return null;

    const documentHash = this.getDocumentDigest(book);
    if (!documentHash) return null;

    try {
      const response = await this.request(`/syncs/progress/${documentHash}`);

      if (!response.ok) {
        console.error(
          `KOSync: Failed to get progress for ${book.title}. Status: ${response.status}`,
        );
        return null;
      }

      const data = await response.json();
      return data.document ? data : null;
    } catch (e) {
      console.error('KOSync getProgress failed', e);
      return null;
    }
  }

  /**
   * Updates the reading progress for a specific book on the server
   * @param book - The book to update progress for
   * @param progress - The current reading progress position
   * @param percentage - The reading completion percentage
   * @returns Promise with boolean indicating success
   */
  async updateProgress(book: Book, progress: string, percentage: number): Promise<boolean> {
    if (!this.config.userkey) return false;

    const documentHash = this.getDocumentDigest(book);
    if (!documentHash) return false;

    const payload = {
      document: documentHash,
      progress,
      percentage,
      device: this.config.deviceName,
      device_id: this.config.deviceId,
    };

    try {
      const response = await this.request('/syncs/progress', {
        method: 'PUT',
        body: JSON.stringify(payload),
      });

      if (!response.ok) {
        console.error(
          `KOSync: Failed to update progress for ${book.title}. Status: ${response.status}`,
        );
        return false;
      }
      return true;
    } catch (e) {
      console.error('KOSync updateProgress failed', e);
      return false;
    }
  }

  getDocumentDigest(book: Book): string {
    if (this.config.checksumMethod === 'filename') {
      console.warn('This is not possible anymore, using md5 instead.');
    }
    return book.hash;
  }
}