This repository was archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathfilesystem.dart
101 lines (88 loc) · 3.2 KB
/
filesystem.dart
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
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// This is a port of "Exploring the FileSystem APIs" to Dart.
// See: http://www.html5rocks.com/en/tutorials/file/filesystem/
import 'dart:convert' show HtmlEscape;
import 'dart:html';
class FileSystemExample {
FileSystem _filesystem;
Element _fileList;
HtmlEscape sanitizer = new HtmlEscape();
FileSystemExample() {
_fileList = querySelector('#example-list-fs-ul');
window.requestFileSystem(1024 * 1024, persistent: false)
.then(_requestFileSystemCallback, onError: _handleError);
}
void _requestFileSystemCallback(FileSystem filesystem) {
_filesystem = filesystem;
querySelector('#add-button').onClick.listen((e) => _addFiles());
querySelector('#list-button').onClick.listen((e) => _listFiles());
querySelector('#remove-button').onClick.listen((e) => _removeFiles());
}
void _handleError(FileError e) {
var msg = '';
switch (e.code) {
case FileError.QUOTA_EXCEEDED_ERR:
msg = 'QUOTA_EXCEEDED_ERR';
break;
case FileError.NOT_FOUND_ERR:
msg = 'NOT_FOUND_ERR';
break;
case FileError.SECURITY_ERR:
msg = 'SECURITY_ERR';
break;
case FileError.INVALID_MODIFICATION_ERR:
msg = 'INVALID_MODIFICATION_ERR';
break;
case FileError.INVALID_STATE_ERR:
msg = 'INVALID_STATE_ERR';
break;
default:
msg = 'Unknown Error';
break;
}
querySelector("#example-list-fs-ul").text = "Error: $msg";
}
void _addFiles() {
_filesystem.root.createFile('log.txt').catchError(_handleError);
_filesystem.root.createFile('song.mp3').catchError(_handleError);
_filesystem.root.createDirectory('mypictures').catchError(_handleError);
_fileList.text = 'Files created.';
}
void _listFiles() {
var dirReader = _filesystem.root.createReader();
dirReader.readEntries().then((entries) {
if (entries.length == 0) {
_fileList.text = 'Filesystem is empty.';
} else {
_fileList.text = '';
}
var fragment = document.createDocumentFragment();
entries.forEach((entry) {
var img = entry.isDirectory ? '<img src="http://www.html5rocks.com/static/images/tutorials/icon-folder.gif">' :
'<img src="http://www.html5rocks.com/static/images/tutorials/icon-file.gif">';
var li = new Element.tag("li");
li.innerHtml = "$img<span>${sanitizer.convert(entry.name)}</span>";
fragment.nodes.add(li);
});
_fileList.nodes.add(fragment);
}, onError: _handleError);
}
void _removeFiles() {
var dirReader = _filesystem.root.createReader();
dirReader.readEntries().then((entries) {
entries.forEach((entry) {
if (entry.isDirectory) {
entry.removeRecursively().then((_) {}, onError: _handleError);
} else {
entry.remove().then((_) {}, onError: _handleError);
}
});
_fileList.text = 'Directory emptied.';
}, onError: _handleError);
}
}
void main() {
new FileSystemExample();
}