-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature:complete niupic image upload
- Loading branch information
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import 'package:dio/dio.dart'; | ||
import 'package:flutter_picgo/utils/net.dart'; | ||
|
||
class NiupicApi { | ||
static const BASE_URL = 'https://www.niupic.com/'; | ||
|
||
static Future upload(FormData data) async { | ||
Response res = await NetUtils.getInstance() | ||
.post(BASE_URL + 'index/upload/process', data: data); | ||
return res.data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import 'package:dio/dio.dart'; | ||
import 'package:flutter_picgo/api/niupic_api.dart'; | ||
import 'package:flutter_picgo/model/uploaded.dart'; | ||
import 'package:flutter_picgo/resources/pb_type_keys.dart'; | ||
import 'package:flutter_picgo/utils/image_upload.dart'; | ||
import 'dart:io'; | ||
|
||
import 'package:flutter_picgo/utils/strategy/image_upload_strategy.dart'; | ||
|
||
class NiupicImageUpload implements ImageUploadStrategy { | ||
/// 牛图网不支持删除 | ||
@override | ||
Future<Uploaded> delete(Uploaded uploaded) async { | ||
return uploaded; | ||
} | ||
|
||
@override | ||
Future<Uploaded> upload(File file, String renameImage) async { | ||
FormData formData = FormData.fromMap({ | ||
"image_field": | ||
await MultipartFile.fromFile(file.path, filename: renameImage), | ||
}); | ||
var result = await NiupicApi.upload(formData); | ||
if (result["code"] == 200) { | ||
var uploadedItem = Uploaded( | ||
-1, 'https://${result['data']}', PBTypeKeys.niupic, | ||
info: ''); | ||
await ImageUploadUtils.saveUploadedItem(uploadedItem); | ||
return uploadedItem; | ||
} else { | ||
throw new NiupicError(error: '${result['msg']}'); | ||
} | ||
} | ||
} | ||
|
||
class NiupicError implements Exception { | ||
NiupicError({ | ||
this.error, | ||
}); | ||
|
||
dynamic error; | ||
|
||
String get message => (error?.toString() ?? ''); | ||
|
||
@override | ||
String toString() { | ||
var msg = 'NiupicError $message'; | ||
if (error is Error) { | ||
msg += '\n${error.stackTrace}'; | ||
} | ||
return msg; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters