@@ -128,61 +128,68 @@ <h1>Android File System Demo</h1>
128128 // 3. OPEN DIRECTORY (Read/Write)
129129 document . getElementById ( 'btnDir' ) . addEventListener ( 'click' , async ( ) => {
130130 try {
131- log ( "Requesting directory access (ReadWrite)..." ) ;
131+ log ( "Requesting directory access..." ) ;
132+ // 1. Get the handle (Android often defaults this to Read-Only!)
132133 const dirHandle = await window . showDirectoryPicker ( {
133134 mode : "readwrite"
134135 } ) ;
135136
136137 log ( `📂 Directory selected: ${ dirHandle . name } ` ) ;
137138
138- // List contents and count files
139+ // --- THE CRITICAL FIX ---
140+ // We must explicitly verify/request WRITE permission now.
141+ const options = { mode : 'readwrite' } ;
142+ // Check if we already have permission
143+ if ( ( await dirHandle . queryPermission ( options ) ) !== 'granted' ) {
144+ log ( "⚠️ Handle is Read-Only. Requesting Write access..." ) ;
145+ // This triggers the browser prompt: "Allow site to edit files?"
146+ if ( ( await dirHandle . requestPermission ( options ) ) !== 'granted' ) {
147+ throw new Error ( "Write permission was denied by the user." ) ;
148+ }
149+ }
150+ log ( "✅ Write permission confirmed!" ) ;
151+ // -------------------------
152+
153+ // List contents
139154 const entries = [ ] ;
140155 log ( "--- Listing Files ---" ) ;
141156 for await ( const entry of dirHandle . values ( ) ) {
142157 entries . push ( entry ) ;
143158 log ( ` - ${ entry . kind } : ${ entry . name } ` ) ;
144159 }
145- if ( entries . length === 0 ) {
146- log ( " (empty folder)" ) ;
147- }
160+ if ( entries . length === 0 ) log ( " (empty folder)" ) ;
148161
149- // Check if session-log.txt exists and read its content
162+ // Write/append to session log
150163 let existingLog = "" ;
151- let visitCount = 1 ;
152164 try {
153165 const existingHandle = await dirHandle . getFileHandle ( 'session-log.txt' , { create : false } ) ;
154166 const file = await existingHandle . getFile ( ) ;
155167 existingLog = await file . text ( ) ;
156- // Count previous visits
157- const matches = existingLog . match ( / V i s i t # \d + / g) ;
158- visitCount = matches ? matches . length + 1 : 1 ;
159- log ( `📖 Found existing session-log.txt (${ visitCount - 1 } previous visits)` ) ;
168+ log ( "📖 Found existing log." ) ;
160169 } catch {
161- log ( "📝 No previous session-log.txt found, creating fresh log..." ) ;
170+ log ( "📝 Creating fresh log..." ) ;
162171 }
163172
164- // Write/append to session log
165173 const timestamp = new Date ( ) . toISOString ( ) ;
166- const newEntry = `\n--- Visit # ${ visitCount } ---\nTimestamp: ${ timestamp } \nFiles found : ${ entries . length } \n` ;
174+ const newEntry = `\n--- Visit ---\nTimestamp: ${ timestamp } \nFiles: ${ entries . length } \n` ;
167175
176+ // Get handle for file
168177 const logHandle = await dirHandle . getFileHandle ( 'session-log.txt' , { create : true } ) ;
178+
179+ // --- SECOND FIX: Verify permission on the FILE handle too ---
180+ // Sometimes the directory has permission, but the new file handle needs it too.
181+ if ( ( await logHandle . queryPermission ( options ) ) !== 'granted' ) {
182+ await logHandle . requestPermission ( options ) ;
183+ }
184+
169185 const logWritable = await logHandle . createWritable ( ) ;
170186 await logWritable . write ( existingLog + newEntry ) ;
171187 await logWritable . close ( ) ;
172- log ( `✅ Updated session-log.txt (Visit #${ visitCount } )` ) ;
173-
174- // Create a unique timestamped file on each visit
175- const uniqueFileName = `test-${ Date . now ( ) } .txt` ;
176- const uniqueHandle = await dirHandle . getFileHandle ( uniqueFileName , { create : true } ) ;
177- const uniqueWritable = await uniqueHandle . createWritable ( ) ;
178- await uniqueWritable . write ( `Created during visit #${ visitCount } at ${ timestamp } ` ) ;
179- await uniqueWritable . close ( ) ;
180- log ( `✅ Created '${ uniqueFileName } '` ) ;
181-
182- log ( `\n🎉 Demo complete! Reload page and select same folder to see persistence.` ) ;
188+
189+ log ( `✅ Success! Wrote to session-log.txt` ) ;
183190
184191 } catch ( err ) {
185- if ( err . name === 'AbortError' ) log ( "User cancelled folder picker ." ) ;
192+ if ( err . name === 'AbortError' ) log ( "User cancelled." ) ;
186193 else log ( `Error: ${ err . message } ` , 'error' ) ;
187194 }
188195 } ) ;
0 commit comments