@@ -13,6 +13,12 @@ class ParseFile extends ParseObject {
1313
1414 String get url => _fileUrl;
1515
16+ File get file => _file;
17+
18+ set url (String url) => _fileUrl = url;
19+
20+ set name (String name) => _fileName = name;
21+
1622 bool get saved => url != null ;
1723
1824 @override
@@ -24,12 +30,53 @@ class ParseFile extends ParseObject {
2430 /// Creates a new file
2531 ///
2632 /// {https://docs.parseplatform.org/rest/guide/#files/}
27- ParseFile (this ._file, {bool debug, ParseHTTPClient client}) : super (keyFile){
33+ ParseFile (this ._file, {String name, String url, bool debug, ParseHTTPClient client}) : super (keyFile){
2834 client == null ? _client = ParseHTTPClient () : _client = client;
2935 _debug = isDebugEnabled (objectLevelDebug: debug);
36+ if (_file != null ) {
37+ this ._fileName = path.basename (_file.path);
38+ this ._path = 'files/$_fileName ' ;
39+ }
40+ else {
41+ this ._fileName = name;
42+ this ._fileUrl = url;
43+ }
44+ }
45+
46+ Future <ParseFile > loadStorage () async {
47+ Directory tempPath = await getTemporaryDirectory ();
48+
49+ if (_fileName == null ) {
50+ _file = null ;
51+ return this ;
52+ }
53+
54+ File possibleFile = new File ("${tempPath .path }/$_fileName " );
55+ bool exists = await possibleFile.exists ();
56+
57+ if (exists) {
58+ _file = possibleFile;
59+ }
60+ else {
61+ _file = null ;
62+ }
63+
64+ return this ;
65+ }
66+
67+ Future <ParseFile > download () async {
68+ if (_fileUrl == null ) {
69+ return this ;
70+ }
71+
72+ Directory tempPath = await getTemporaryDirectory ();
73+ this ._file = new File ("${tempPath .path }/$_fileName " );
74+ await _file.create ();
75+
76+ var response = await _client.get (_fileUrl);
77+ _file.writeAsBytes (response.bodyBytes);
3078
31- this ._fileName = path.basename (_file.path);
32- this ._path = 'files/$_fileName ' ;
79+ return this ;
3380 }
3481
3582 /// Uploads a file to Parse Server
@@ -48,4 +95,4 @@ class ParseFile extends ParseObject {
4895 final response = await _client.post (uri, headers: headers, body: body);
4996 return super .handleResponse <ParseFile >(response, ParseApiRQ .upload);
5097 }
51- }
98+ }
0 commit comments