1
+ part of dart_parse;
2
+
3
+ class ParseFile {
4
+ final Logger _log = new Logger ("ParseFile" );
5
+
6
+ String endPoint;
7
+ bool isDirty = true ;
8
+ String name;
9
+ String url;
10
+ String contentType;
11
+ List <int > data;
12
+
13
+ ParseFile (this .name, {this .data, this .contentType, this .url}) {
14
+ this .endPoint = "files/" ;
15
+ if (data.length > ParseConstant .MAX_PARSE_FILE_SIZE ) {
16
+ throw new ArgumentError ("ParseFile must be less than $ParseConstant .MAX_PARSE_FILE_SIZE bytes, current $data .length" );
17
+ }
18
+ }
19
+
20
+ Future <ParseFile > save () {
21
+ var completer = new Completer ();
22
+ if (isDirty && data != null ) {
23
+ ParseUploadCommand command = new ParseUploadCommand (endPoint + name);
24
+ command.setData (data);
25
+ if (contentType == null ) {
26
+ String possibleFileExtension = mime (name);
27
+ contentType = possibleFileExtension == null ? "text/plain" : possibleFileExtension;
28
+ }
29
+ command.setContentType (contentType);
30
+ command.perform ().then ((ParseResponse response) {
31
+ JsonObject jsonResponse = response.getJsonObject ();
32
+ if (! response.isFailed () && jsonResponse != null ) {
33
+ url = jsonResponse["url" ];
34
+ name = jsonResponse["name" ];
35
+ isDirty = false ;
36
+ completer.complete (this );
37
+ } else {
38
+ completer.completeError (response.getException ());
39
+ }
40
+ }, onError: completer.completeError);
41
+
42
+ } else {
43
+ completer.complete (this );
44
+ }
45
+ return completer.future;
46
+ }
47
+
48
+ //TODO
49
+ Future <ParseFile > getData () {
50
+ var completer = new Completer ();
51
+ completer.complete (this );
52
+ return completer.future;
53
+ }
54
+
55
+ Future <bool > delete () {
56
+ var completer = new Completer ();
57
+ ParseDeleteCommand command = new ParseDeleteCommand (endPoint + name);
58
+ command.perform ().then ((ParseResponse response) {
59
+ name = null ;
60
+ url = null ;
61
+ data = null ;
62
+ isDirty = false ;
63
+ if (response.isFailed ()) {
64
+ completer.completeError (response.getException ());
65
+ } else {
66
+ completer.complete (true );
67
+ }
68
+ }, onError: completer.completeError);
69
+ return completer.future;
70
+ }
71
+
72
+ }
0 commit comments