Skip to content

Commit

Permalink
feature:complete niupic image upload
Browse files Browse the repository at this point in the history
  • Loading branch information
hackycy committed Jul 22, 2020
1 parent 0f901b0 commit 6459b87
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/api/niupic_api.dart
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;
}
}
53 changes: 53 additions & 0 deletions lib/utils/strategy/impl/niupic_image_upload.dart
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;
}
}
4 changes: 4 additions & 0 deletions lib/utils/strategy/upload_strategy_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter_picgo/utils/strategy/impl/aliyun_image_upload.dart';
import 'package:flutter_picgo/utils/strategy/impl/gitee_image_upload.dart';
import 'package:flutter_picgo/utils/strategy/impl/github_image_upload.dart';
import 'package:flutter_picgo/utils/strategy/image_upload_strategy.dart';
import 'package:flutter_picgo/utils/strategy/impl/niupic_image_upload.dart';
import 'package:flutter_picgo/utils/strategy/impl/qiniu_image_upload.dart';
import 'package:flutter_picgo/utils/strategy/impl/smms_image_upload.dart';
import 'package:flutter_picgo/utils/strategy/impl/tcyun_image_upload.dart';
Expand Down Expand Up @@ -35,6 +36,9 @@ class UploadStrategyFactory {
} else if (type == PBTypeKeys.tcyun) {
/// 腾讯云
cache[type] = new TcyunImageUpload();
} else if (type == PBTypeKeys.niupic) {
/// 牛图网
cache[type] = new NiupicImageUpload();
}
}
return cache[type];
Expand Down

0 comments on commit 6459b87

Please sign in to comment.