summaryrefslogtreecommitdiffhomepage
path: root/src/lib/flashair/client.ts
blob: 4e3a5677ab21cbd79d9f296e9ae4492a1c06a3ad (plain)
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
import type { FlashAirFileEntry, ThumbnailMeta } from './types';

/** Parse a 16-bit FAT date + time into a JS Date. */
function parseFatDateTime(fatDate: number, fatTime: number): Date {
  const year = ((fatDate >> 9) & 0x7f) + 1980;
  const month = (fatDate >> 5) & 0x0f;
  const day = fatDate & 0x1f;
  const hour = (fatTime >> 11) & 0x1f;
  const minute = (fatTime >> 5) & 0x3f;
  const second = (fatTime & 0x1f) * 2;
  return new Date(year, month - 1, day, hour, minute, second);
}

/** Attribute bit 4 indicates a directory. */
function isDirectory(attribute: number): boolean {
  return (attribute & 0x10) !== 0;
}

/**
 * Resolve the base URL for FlashAir API requests.
 *
 * In production (served from the card), all requests go to the same origin.
 * In development, we use the Vite dev server origin (which can proxy if configured).
 */
function getBaseUrl(): string {
  return '';
}

/**
 * Parse the text response from command.cgi op=100 into structured entries.
 *
 * First line is the header "WLANSD_FILELIST" and is skipped.
 * Each subsequent line: <directory>,<filename>,<size>,<attribute>,<date>,<time>
 * Note: filenames may contain commas, so we split from the right for the numeric fields.
 */
function parseFileList(text: string): FlashAirFileEntry[] {
  const lines = text.trim().split('\n');
  const entries: FlashAirFileEntry[] = [];

  for (let i = 1; i < lines.length; i++) {
    const line = lines[i];
    if (line === undefined || line.trim() === '') continue;

    // Split from the right: the last 4 fields are always numeric (size, attribute, date, time).
    // The first field is directory, and everything between is the filename (which may contain commas).
    const parts = line.split(',');
    if (parts.length < 6) continue;

    const rawTime = Number(parts[parts.length - 1]);
    const rawDate = Number(parts[parts.length - 2]);
    const attribute = Number(parts[parts.length - 3]);
    const size = Number(parts[parts.length - 4]);
    const directory = parts[0] ?? '';
    // Filename is everything between the first and last 4 fields
    const filename = parts.slice(1, parts.length - 4).join(',');

    const date = parseFatDateTime(rawDate, rawTime);
    const dir = isDirectory(attribute);
    const path = directory === '' ? `/${filename}` : `${directory}/${filename}`;

    entries.push({
      directory,
      filename,
      size,
      attribute,
      rawDate,
      rawTime,
      date,
      isDirectory: dir,
      path,
    });
  }

  return entries;
}

/** Known image extensions the FlashAir thumbnail.cgi supports (JPEG only). */
const JPEG_EXTENSIONS = new Set(['.jpg', '.jpeg']);

function isJpeg(filename: string): boolean {
  const ext = filename.slice(filename.lastIndexOf('.')).toLowerCase();
  return JPEG_EXTENSIONS.has(ext);
}

/**
 * FlashAir API client.
 *
 * All methods return promises and throw on network/HTTP errors.
 */
export const flashair = {
  /**
   * List files in a directory.
   * @param dir - Absolute path on the card, e.g. "/DCIM"
   */
  async listFiles(dir: string): Promise<FlashAirFileEntry[]> {
    const base = getBaseUrl();
    const res = await fetch(`${base}/command.cgi?op=100&DIR=${encodeURIComponent(dir)}`);
    if (!res.ok) throw new Error(`listFiles failed: ${res.status} ${res.statusText}`);
    const text = await res.text();
    return parseFileList(text);
  },

  /**
   * Get the number of files in a directory.
   */
  async getFileCount(dir: string): Promise<number> {
    const base = getBaseUrl();
    const res = await fetch(`${base}/command.cgi?op=101&DIR=${encodeURIComponent(dir)}`);
    if (!res.ok) throw new Error(`getFileCount failed: ${res.status} ${res.statusText}`);
    const text = await res.text();
    return Number(text.trim());
  },

  /**
   * Check if the card memory has been updated since last check.
   * Returns true if updated. Calling this clears the flag.
   */
  async hasUpdated(): Promise<boolean> {
    const base = getBaseUrl();
    const res = await fetch(`${base}/command.cgi?op=102`);
    if (!res.ok) throw new Error(`hasUpdated failed: ${res.status} ${res.statusText}`);
    const text = await res.text();
    return text.trim() === '1';
  },

  /**
   * Get the firmware version string.
   */
  async getFirmwareVersion(): Promise<string> {
    const base = getBaseUrl();
    const res = await fetch(`${base}/command.cgi?op=108`);
    if (!res.ok) throw new Error(`getFirmwareVersion failed: ${res.status} ${res.statusText}`);
    return (await res.text()).trim();
  },

  /**
   * Get SSID of the FlashAir.
   */
  async getSSID(): Promise<string> {
    const base = getBaseUrl();
    const res = await fetch(`${base}/command.cgi?op=104`);
    if (!res.ok) throw new Error(`getSSID failed: ${res.status} ${res.statusText}`);
    return (await res.text()).trim();
  },

  /**
   * Get the number of free sectors on the card.
   */
  async getFreeSectors(): Promise<number> {
    const base = getBaseUrl();
    const res = await fetch(`${base}/command.cgi?op=140`);
    if (!res.ok) throw new Error(`getFreeSectors failed: ${res.status} ${res.statusText}`);
    return Number((await res.text()).trim());
  },

  /**
   * Get the URL for a file on the card.
   */
  fileUrl(path: string): string {
    return `${getBaseUrl()}${path}`;
  },

  /**
   * Get the thumbnail URL for a JPEG file.
   * Returns undefined if the file is not a JPEG (thumbnails only work for JPEGs).
   */
  thumbnailUrl(path: string): string | undefined {
    const filename = path.slice(path.lastIndexOf('/') + 1);
    if (!isJpeg(filename)) return undefined;
    return `${getBaseUrl()}/thumbnail.cgi?${path}`;
  },

  /**
   * Fetch a thumbnail and its EXIF metadata.
   * Returns the blob and optional EXIF dimensions/orientation.
   */
  async fetchThumbnail(path: string): Promise<{ blob: Blob; meta: ThumbnailMeta }> {
    const url = flashair.thumbnailUrl(path);
    if (url === undefined) throw new Error(`Not a JPEG file: ${path}`);
    const res = await fetch(url);
    if (!res.ok) throw new Error(`fetchThumbnail failed: ${res.status} ${res.statusText}`);
    const blob = await res.blob();

    const widthStr = res.headers.get('X-exif-WIDTH');
    const heightStr = res.headers.get('X-exif-HEIGHT');
    const orientStr = res.headers.get('X-exif-ORIENTATION');

    const meta: ThumbnailMeta = {
      width: widthStr !== null ? Number(widthStr) : undefined,
      height: heightStr !== null ? Number(heightStr) : undefined,
      orientation: orientStr !== null ? Number(orientStr) : undefined,
    };

    return { blob, meta };
  },
} as const;

export type { FlashAirFileEntry, ThumbnailMeta } from './types';