@@ -78,56 +78,92 @@ export async function resolveDataUrl(value, opts = {}) {
78
78
}
79
79
blob = await compressImage ( blob , maxSizeKb ) ;
80
80
}
81
- if ( opts . opfsName ) {
82
- await opfsWrite ( value , opts . opfsName ) ;
81
+ if ( opts . opfsDir && opts . opfsFile ) {
82
+ await opfsWrite ( value , opts ) ;
83
83
}
84
84
return URL . createObjectURL ( blob ) ;
85
85
} catch ( e ) {
86
86
return value ;
87
87
}
88
88
}
89
89
90
- export async function opfsWrite ( value , opfsName ) {
90
+ export async function opfsWrite ( value , { opfsDir , opfsFile } ) {
91
91
try {
92
92
const root = await navigator . storage . getDirectory ( ) ;
93
- const handle = await root . getFileHandle ( opfsName , { create : true } ) ;
94
- const stream = await handle . createWritable ( ) ;
93
+ const dir = await root . getDirectoryHandle ( opfsDir , { create : true } ) ;
94
+ const file = await dir . getFileHandle ( opfsFile , { create : true } ) ;
95
+ const stream = await file . createWritable ( ) ;
95
96
await stream . write ( value ) ;
96
97
await stream . close ( ) ;
97
98
} catch ( e ) {
98
- alert ( "OPFS write failure: " + e . toString ( ) + " file: " + opfsName ) ;
99
+ alert (
100
+ "OPFS write failure: " +
101
+ e . toString ( ) +
102
+ " dir: " +
103
+ opfsDir +
104
+ " file: " +
105
+ opfsFile ,
106
+ ) ;
99
107
}
100
108
return null ;
101
109
}
102
110
103
- export async function opfsRead ( opfsName ) {
111
+ export async function opfsRead ( { opfsDir , opfsFile } ) {
104
112
try {
105
113
const root = await navigator . storage . getDirectory ( ) ;
106
- const handle = await root . getFileHandle ( opfsName ) ;
107
- const file = await handle . getFile ( ) ;
108
- const uri = await file . text ( ) ;
114
+ const dir = await root . getDirectoryHandle ( opfsDir ) ;
115
+ const file = await dir . getFileHandle ( opfsFile ) ;
116
+ const blob = await file . getFile ( ) ;
117
+ const uri = await blob . text ( ) ;
109
118
const res = await resolveDataUrl ( uri ) ;
110
119
return res ;
111
120
} catch ( e ) {
112
- alert ( "OPFS read failure: " + e . toString ( ) + " file: " + opfsName ) ;
121
+ alert (
122
+ "OPFS read failure: " +
123
+ e . toString ( ) +
124
+ " dir: " +
125
+ opfsDir +
126
+ " file: " +
127
+ opfsFile ,
128
+ ) ;
113
129
return null ;
114
130
}
115
131
}
116
132
117
- export async function opfsList ( ) {
133
+ export async function opfsList ( opfsDir ) {
118
134
try {
119
135
const res = [ ] ;
120
136
const root = await navigator . storage . getDirectory ( ) ;
121
- for await ( let opfsName of root . keys ( ) ) {
122
- res . push ( opfsName ) ;
137
+ const dir = await root . getDirectoryHandle ( opfsDir ) ;
138
+ for await ( let opfsFile of dir . keys ( ) ) {
139
+ res . push ( opfsFile ) ;
123
140
}
124
141
return res ;
125
142
} catch ( e ) {
126
- alert ( "OPFS list failure: " + e . toString ( ) + " file : " + opfsName ) ;
143
+ alert ( "OPFS list failure: " + e . toString ( ) + " dir : " + opfsDir ) ;
127
144
return [ ] ;
128
145
}
129
146
}
130
147
148
+ export async function opfsRemove ( { opfsDir, opfsFile } ) {
149
+ try {
150
+ const root = await navigator . storage . getDirectory ( ) ;
151
+ const dir = await root . getDirectoryHandle ( opfsDir ) ;
152
+ const file = await dir . getFileHandle ( opfsFile ) ;
153
+ await file . remove ( ) ;
154
+ } catch ( e ) {
155
+ alert (
156
+ "OPFS remove failure: " +
157
+ e . toString ( ) +
158
+ " dir: " +
159
+ opfsDir +
160
+ " file: " +
161
+ opfsFile ,
162
+ ) ;
163
+ }
164
+ return null ;
165
+ }
166
+
131
167
export async function openBrowserPage ( url ) {
132
168
try {
133
169
return await Browser . open ( { url : url , windowName : "_blank" } ) ;
0 commit comments