Skip to content

Commit 35a72d1

Browse files
committed
Adjust the upload interface a little now that we 'upload' two files in the Inform 7 template
1 parent a698aa0 commit 35a72d1

5 files changed

Lines changed: 22 additions & 20 deletions

File tree

src/common/file/browser.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,21 @@ export async function read_response(response: Response, progress_callback?: Prog
152152
return result
153153
}
154154

155-
/** A helper class to handle requesting JSONP resources one at a time */
155+
/** Read an uploaded file and return it as a Uint8Array */
156+
export function read_uploaded_file(file: File): Promise<Uint8Array<ArrayBuffer>> {
157+
return new Promise((resolve, reject) => {
158+
const reader = new FileReader()
159+
reader.onerror = () => reject(reader.error)
160+
reader.onload = () => resolve(new Uint8Array(reader.result as ArrayBuffer))
161+
reader.readAsArrayBuffer(file)
162+
})
163+
}
164+
156165
interface QueueRequest<T> {
157166
path: string,
158167
resolve: (value: T) => void,
159168
}
169+
/** A helper class to handle requesting JSONP resources one at a time */
160170
class JSONPQueue {
161171
private options: DownloadOptions
162172
private pending = false

src/dialog/browser/browser.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ https://github.com/curiousdannii/asyncglk
1212
import {saveAs as filesave_saveAs} from 'file-saver'
1313
import path from 'path-browserify-esm'
1414

15+
import {read_uploaded_file} from '../../common/file/browser.js'
1516
import type {DownloadOptions, ProgressCallback} from '../../common/file/interface.js'
1617
import type {DialogDirectories, DialogOptions} from '../common/interface.js'
1718
import {show_alert} from './common.js'
18-
import {DownloadProvider, read_uploaded_file} from './download.js'
19+
import {DownloadProvider} from './download.js'
1920
import type {BrowseableProvider, BrowserDialog, DirEntry, FilesMetadata, Provider} from './interface.js'
2021
import {WebStorageProvider} from './storage.js'
2122
import FileDialog from './ui/FileDialog.svelte'
@@ -85,9 +86,11 @@ export class ProviderBasedBrowserDialog implements BrowserDialog {
8586
}
8687
}
8788

88-
async upload(file: File) {
89-
const file_path = await this.downloader!.upload(file)
90-
this.setup(file_path)
89+
async upload(filename: string, data: Uint8Array<ArrayBuffer>, main_file = true) {
90+
const file_path = await this.downloader!.upload(filename, data)
91+
if (main_file) {
92+
this.setup(file_path)
93+
}
9194
return file_path
9295
}
9396

src/dialog/browser/download.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ export class DownloadProvider implements Provider {
3636
return path
3737
}
3838

39-
async upload(file: File) {
40-
const data = await read_uploaded_file(file)
41-
const path = '/upload/' + file.name
39+
async upload(filename: string, data: Uint8Array<ArrayBuffer>) {
40+
const path = '/upload/' + filename
4241
this.store.set(path, data)
4342
return path
4443
}
@@ -164,16 +163,6 @@ async function fetch_storyfile(options: DownloadOptions, url: string, progress_c
164163
return [url, data]
165164
}
166165

167-
/** Read an uploaded file and return it as a Uint8Array */
168-
export function read_uploaded_file(file: File): Promise<Uint8Array<ArrayBuffer>> {
169-
return new Promise((resolve, reject) => {
170-
const reader = new FileReader()
171-
reader.onerror = () => reject(reader.error)
172-
reader.onload = () => resolve(new Uint8Array(reader.result as ArrayBuffer))
173-
reader.readAsArrayBuffer(file)
174-
})
175-
}
176-
177166
function url_to_path(url: string) {
178167
if (url.startsWith('https:')) {
179168
return '/download/https/' + url.substring(8)

src/dialog/browser/interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type {AsyncDialog} from '../common/interface.js'
1414

1515
export interface BrowserDialog extends AsyncDialog {
1616
download(url: string, progress_callback?: ProgressCallback): Promise<string>
17-
upload(file: File): Promise<string>
17+
upload(filename: string, data: Uint8Array<ArrayBuffer>, main_file?: boolean): Promise<string>
1818
}
1919

2020
/** A provider handles part of the filesystem, and can cascade down to another provider for files it doesn't handle */

src/index-browser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ https://github.com/curiousdannii/asyncglk
1111

1212
export * from './index-common.js'
1313

14-
export {fetch_resource, parse_base64, process_resource, read_response} from './common/file/browser.js'
14+
export {fetch_resource, parse_base64, process_resource, read_response, read_uploaded_file} from './common/file/browser.js'
1515

1616
export {ProviderBasedBrowserDialog} from './dialog/browser/browser.js'
1717

0 commit comments

Comments
 (0)