Skip to content

Commit

Permalink
Revert TujianApi#uploadFile
Browse files Browse the repository at this point in the history
玄学,不敢动不敢动...
  • Loading branch information
神楽坂花火 committed Mar 26, 2020
1 parent e84a36d commit f289baa
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 31 deletions.
1 change: 1 addition & 0 deletions android/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions lib/pages/upload.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import 'dart:convert';
import 'dart:io';
import 'dart:ui';

Expand Down Expand Up @@ -269,7 +268,10 @@ class _UploadPageState extends State<UploadPage> {
actions: <Widget>[
CupertinoDialogAction(
child: const Text('好'),
onPressed: () => Navigator.of(context).pop(),
onPressed: () {
setState(() => progress = -1);
Navigator.of(context).pop();
},
)
],
);
Expand Down Expand Up @@ -312,28 +314,27 @@ class _UploadPageState extends State<UploadPage> {
}

setState(() => progress = null);
dynamic json = jsonDecode(await TujianApi.uploadFile(
dynamic json = await TujianApi.uploadFile(
imageFile,
(int count, int total) {
setState(() => progress = count / total);
},
));
);
if (!json['ret']) {
await _showAlertDialog(json['error']['message']);
return;
}
String result = await TujianApi.submit(
dynamic result = await TujianApi.submit(
title: title.text,
content: content.text,
url: 'https://img.dpic.dev/' + json['info']['md5'],
user: username.text,
type: type,
email: email.text,
);
dynamic json2 = jsonDecode(result);
if (json2['code'] != 200) {
if (result['code'] != 200) {
setState(() => progress = -1);
await _showAlertDialog('投稿失败,因为:' + json2['msg']);
await _showAlertDialog('投稿失败,因为:' + result['msg']);
} else {
await _showAlertDialog('投稿成功,请等待管理员审核');
Navigator.of(context).pop();
Expand Down
46 changes: 24 additions & 22 deletions lib/utils/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:dailypics/misc/bean.dart';
Expand Down Expand Up @@ -89,27 +90,34 @@ class TujianApi {
return Splash.fromJson(response.data);
}

static Future<String> uploadFile(
static Future<dynamic> uploadFile(
File file,
ProgressCallback onProgress,
) async {
Uri uri = Uri.parse('https://img.dpic.dev/upload');
HttpClient client = HttpClient();
HttpClientRequest request = await client.postUrl(uri);
String subType = file.path.substring(file.path.lastIndexOf('.') + 1);
Response response = await http.postUri(
Uri.parse('https://img.dpic.dev/upload'),
onSendProgress: onProgress,
data: file.openRead(),
options: Options(
responseType: ResponseType.plain,
headers: {
'Content-Type': 'image/$subType',
'Content-Length': file.lengthSync(),
},
),
);
return response.data;
request.headers.set('content-type', 'image/$subType');
int contentLength = file.statSync().size;
int byteCount = 0;
Stream<List<int>> stream = file.openRead();
await request.addStream(stream.transform(StreamTransformer.fromHandlers(
handleDone: (sink) => sink.close(),
handleError: (_, __, ___) {},
handleData: (data, sink) {
byteCount += data.length;
sink.add(data);
if (onProgress != null) {
onProgress(byteCount, contentLength);
}
},
)));
HttpClientResponse response = await request.close();
return jsonDecode(await response.cast<List<int>>().transform(utf8.decoder).join());
}

static Future<String> submit({
static Future<dynamic> submit({
String title,
String content,
String url,
Expand All @@ -125,13 +133,7 @@ class TujianApi {
'sort': type,
'hz': email,
};
Response response = await http.postUri(
Uri.parse('$_kBaseUrl/tg'),
data: data,
options: Options(
responseType: ResponseType.plain,
)
);
Response response = await http.postUri(Uri.parse('$_kBaseUrl/tg'), data: data);
return response.data;
}
}
2 changes: 1 addition & 1 deletion lib/utils/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import 'package:dio/dio.dart';

Dio http = Dio(BaseOptions(
headers: {
'User-Agent': 'Dailypics/${Config.version} Version/${Config.buildNumber}',
'user-agent': 'Dailypics/${Config.version} Version/${Config.buildNumber}',
},
));

0 comments on commit f289baa

Please sign in to comment.