@@ -12,11 +12,25 @@ import 'package:http/http.dart' as http;
1212import 'package:path_provider/path_provider.dart' ;
1313
1414
15+ import 'constants.dart' ;
16+ import '../Models/uploadActivity.dart' ;
1517import 'strava.dart' ; // To remove when removing test1 and test2
1618
19+ // To check if the activity has been uploaded successfully
20+ // No numeric error code for the moment given by Strava
21+ class StatusUpload {
22+ String ready = "Your activity is ready." ;
23+ String deleted = "The created activity has been deleted." ;
24+ String error = "There was an error processing your activity." ;
25+ String processed = "Your activity is still being processed." ;
26+ String notFound = 'Not Found' ;
27+
28+ } // End of enum statusUpload
29+
30+
1731
1832abstract class Upload {
19- // Upload();
33+
2034
2135 Future <String > get _localPath async {
2236 final directory = await getApplicationDocumentsDirectory ();
@@ -51,10 +65,12 @@ abstract class Upload {
5165 buffer.asUint8List (data.offsetInBytes, data.lengthInBytes));
5266 }
5367
54- void uploadActivity (String name, String description, String fileUrl,
68+ Future < Fault > uploadActivity (String name, String description, String fileUrl,
5569 String fileType, String accessToken) async {
5670 final postUri = Uri .parse ('https://www.strava.com/api/v3/uploads' );
5771
72+ var fault = Fault (888 , '' );
73+
5874 var request = http.MultipartRequest ("POST" , postUri);
5975 request.fields['data_type' ] = fileType; // tested with gpx
6076 request.fields['trainer' ] = 'false' ;
@@ -68,38 +84,60 @@ abstract class Upload {
6884
6985 request.files.add (await http.MultipartFile .fromPath ('file' , fileUrl));
7086
71- // var response = await request.send() ;
87+ String _body = '' ;
7288
73- // Not working
74- // var response = await request.send();
75- // print('Response: ${response.statusCode} ${response.reasonPhrase}');
76- // String _body = await response.stream.transform(utf8.decoder).join();
89+ var response = await request.send ();
7790
78- // Working
79- String _body = '' ;
80- request.send ().then ((response) {
91+ // request.send().then((response) {
8192 print ('Response: ${response .statusCode } ${response .reasonPhrase }' );
93+
94+ fault.statusCode = response.statusCode;
95+ fault.message = response.reasonPhrase;
96+
8297 if (response.statusCode == 201 ) {
8398 print ('Created!!!' );
99+ } else {
100+ print ('Error while uploading the activity' );
101+ print ('---> ${response .reasonPhrase }' );
102+
84103 }
85104
86- // if statusCode == 400 and "Bad Request" Most probbaly the activity has been alreacdy created
105+ int idUpload;
106+
107+ // Upload is processed by the server
108+ if (fault.statusCode == 201 ) {
87109
88110 response.stream.transform (utf8.decoder).listen ((value) {
89111 print (value);
90- _body = _body + value;
112+ var _body = json.decode (value);
113+ ResponseUploadActivity _response = ResponseUploadActivity .fromJson (_body);
114+
115+ print ('id ${_response .id }' );
116+ idUpload = _response.id;
117+
91118 });
92- });
93119
94- // Not working yet
95- /****
96- final jsonResponse = json.decode(_body);
97- final UploadActivity _uploadActivity =
98- UploadActivity.fromJson(jsonResponse);
99- var _activityID = _uploadActivity.id;
100- print('activity $_activityID');
101- ***/
120+
121+ // Todo: Every second or two check if upload has been susccesful
122+
123+ final reqCheckUpgrade = 'https://www.strava.com/api/v3/uploads/' + idUpload.toString ();
124+
125+ var resp = await http.get (reqCheckUpgrade, headers: header);
126+ print ('check status ${resp .reasonPhrase }' );
127+
128+
129+
130+ }
131+
132+ return fault;
133+
134+
102135 }
136+
137+
138+
139+
140+ //// test2()
103141 /// This is a test function
104142 /// to debug Strava upload activity
105143 void test2 (String secret) async {
@@ -108,9 +146,6 @@ abstract class Upload {
108146 "32212" , secret, "http://localhost:8080" , 'auto' , 'activity:write' );
109147
110148 await strava.OAuth ("32212" , "http://localhost:8080" , 'activity:write' , secret);
111- // var token = Token();
112- // var _token = await token.getStoredToken();
113-
114149 var _token = await strava.getStoredToken ();
115150
116151 // Use the asset file to test without having to create internally a ride
@@ -133,8 +168,6 @@ abstract class Upload {
133168 var text = await readFile ();
134169 print ('text ?? $text ' );
135170
136- // var currentDir = Directory.current;
137-
138171 String dir = (await getApplicationDocumentsDirectory ()).path;
139172
140173 var newFile = File ('$dir /tototo.txt' );
@@ -153,8 +186,6 @@ abstract class Upload {
153186 var newAct = File ('$dir /myActivity.gpx' );
154187 print (newAct.lengthSync ());
155188
156- // Will not work for asset file
157- // var fileActivity = File('assets/test.JPG');
158189
159190 final strava = Strava (
160191 "32212" , secret, "http://localhost:8080" , 'auto' , 'activity:write' );
@@ -189,17 +220,6 @@ abstract class Upload {
189220 }
190221 });
191222
192- /****
193- var documentsDir = await getApplicationDocumentsDirectory();
194- print('documents directory path ${documentsDir.path}');
195-
196223
197-
198- documentsDir
199- .list(recursive: false, followLinks: false)
200- .listen((FileSystemEntity entity) {
201- print(entity.path);
202- });
203- ****/
204224 }
205225}
0 commit comments