flutter app untuk unitstock
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

212 regels
6.6 KiB

  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'dart:typed_data';
  5. import 'package:http/io_client.dart';
  6. import 'package:path_provider/path_provider.dart';
  7. import 'package:http/http.dart';
  8. import '../main.dart';
  9. class file_Trans_Handler {
  10. // double _progress = 0;
  11. String _path = '';
  12. StreamSubscription dlulStream;
  13. String _error = '';
  14. get path => _path;
  15. get error => _error;
  16. StreamController _progress = new StreamController<double>();
  17. Stream<double> get progress =>_progress.stream;
  18. bool useLocal = false;
  19. downloadFile(String fileName,String link) async {
  20. StreamedResponse _response;
  21. List<int> _bytes = [];
  22. int _total = 0;
  23. print('Start Download');
  24. _progress.add(null);
  25. try {
  26. Request req = new Request('GET', Uri.parse(link));
  27. // req.headers = '';
  28. _response = await http.send(req).timeout(
  29. Duration(seconds: 20));
  30. // _response = await client.get('$link',headers: 'application/json')
  31. _total = _response.contentLength;
  32. print('${_total / 1024} KB');
  33. dlulStream = _response.stream.listen((value) {
  34. _bytes.addAll(value);
  35. _progress.add(((_bytes.length / _total)));
  36. })
  37. ..onDone(() async {
  38. _progress.add(0.0);
  39. print('Finish Download');
  40. final file = File(
  41. "${(await getApplicationDocumentsDirectory()).path}/$fileName");
  42. await file.writeAsBytes(_bytes);
  43. _path = file.path;
  44. })
  45. ..onError((e) async {
  46. print('Error Download, $e');
  47. _progress.add(-1.0);
  48. _error = e.toString();
  49. });
  50. }
  51. on HandshakeException catch(e){
  52. if(useLocal){
  53. print('Error Download, $e');
  54. _progress.add(-1.0);
  55. _error = e.toString();
  56. }
  57. else{
  58. useLocal = true;
  59. http = IOClient(HttpClient(context: clientContext));
  60. await downloadFile(fileName,link);
  61. }
  62. }
  63. catch(e){
  64. print('Error Download, $e');
  65. _progress.add(-1.0);
  66. _error = e.toString();
  67. }
  68. }
  69. uploadFile(String fileName,String link,String company,String cabang_id) async{
  70. final file = File(
  71. "${(await getApplicationDocumentsDirectory()).path}/$fileName");
  72. if(file.existsSync()){
  73. Uint8List byte = file.readAsBytesSync();
  74. // print("file size ${file.lengthSync()/1024}");
  75. try{
  76. var _reponse = await http.post(
  77. Uri.parse(link), headers: {'Content-type': 'application/json'},
  78. body: json.encode({"byte":byte,"cabangId":cabang_id,"company":company}));
  79. print('File send ${file.lengthSync()/1024} KB');
  80. final Map data = JsonDecoder().convert(_reponse.body);
  81. // print(_reponse.body);
  82. // if(data['STATUS']==1){
  83. // return {"STATUS":1,"DATA":'File send ${file.lengthSync()/1024} KB'};
  84. // }
  85. // else {
  86. return data;
  87. // }
  88. }
  89. on HandshakeException catch(e){
  90. if(useLocal){
  91. return {"STATUS":0,"DATA":'Request timeout. Make sure server is up and running'};
  92. }
  93. else{
  94. useLocal = true;
  95. http = IOClient(HttpClient(context: clientContext));
  96. return await uploadFile( fileName, link, company, cabang_id);
  97. }
  98. }
  99. catch(e){
  100. print(e);
  101. return {"STATUS":0,"DATA":'Request timeout. Make sure server is up and running'};
  102. }
  103. }
  104. else{
  105. return {"STATUS":0,"DATA":'No such file'};
  106. }
  107. }
  108. unPackDb(String link,String company,String cabang_id,String dbPath) async{
  109. try{
  110. var _reponse = await http.post(
  111. Uri.parse(link), headers: {'Content-type': 'application/json'},
  112. body: json.encode({"cabangId":cabang_id,"company":company,"dbPath":dbPath}));
  113. final Map data = JsonDecoder().convert(_reponse.body);
  114. return data;
  115. }
  116. on HandshakeException catch(e){
  117. if(useLocal){
  118. return {"STATUS":0,"DATA":'Upload timeout. Make sure server is up and running'};
  119. }
  120. else{
  121. useLocal = true;
  122. http = IOClient(HttpClient(context: clientContext));
  123. return await unPackDb( link, company, cabang_id, dbPath);
  124. }
  125. }
  126. catch(e){
  127. print(e);
  128. return {"STATUS":0,"DATA":'Upload timeout. Make sure server is up and running'};
  129. }
  130. }
  131. submitDb(String link,String company,String stock_id) async{
  132. try{
  133. var _reponse = await http.post(
  134. Uri.parse(link), headers: {'Content-type': 'application/json'},
  135. body: json.encode({"stockTakingId":stock_id,"company":company}));
  136. final Map data = JsonDecoder().convert(_reponse.body);
  137. return data;
  138. }
  139. on HandshakeException catch(e){
  140. if(useLocal){
  141. return {"STATUS":0,"DATA":'Request timeout. Make sure server is up and running'};
  142. }
  143. else{
  144. useLocal = true;
  145. http = IOClient(HttpClient(context: clientContext));
  146. return await submitDb(link,company,stock_id);
  147. }
  148. }
  149. catch(e){
  150. print(e);
  151. return {"STATUS":0,"DATA":'Request timeout. Make sure server is up and running'};
  152. }
  153. }
  154. uploadMultipart(String fileName,String link)async{
  155. StreamedResponse _response;
  156. List<int> _bytes = [];
  157. int _total = 0;
  158. try{
  159. var request = MultipartRequest('POST', Uri.parse(link));
  160. request.files.add(
  161. await MultipartFile.fromPath(
  162. 'picture',
  163. "${(await getApplicationDocumentsDirectory()).path}/$fileName"
  164. )
  165. );
  166. _response = await http.send(request);
  167. _total = File("${(await getApplicationDocumentsDirectory()).path}/$fileName").lengthSync();
  168. dlulStream = _response.stream.listen((value) {
  169. _bytes.addAll(value);
  170. print('upload ${_bytes.length/_total}');
  171. _progress.add(((_bytes.length / _total)));
  172. })
  173. ..onDone(() async {
  174. _progress.add(0.0);
  175. print('Finish Download');
  176. final file = File(
  177. "${(await getApplicationDocumentsDirectory()).path}/$fileName");
  178. await file.writeAsBytes(_bytes);
  179. _path = file.path;
  180. })
  181. ..onError((e) async {
  182. print('Error Download, $e');
  183. _progress.add(-1.0);
  184. _error = e.toString();
  185. });
  186. }
  187. on HandshakeException catch(e){
  188. if(useLocal){
  189. print('Error Download, $e');
  190. _progress.add(-1.0);
  191. _error = e.toString();
  192. }
  193. else{
  194. useLocal = true;
  195. http = IOClient(HttpClient(context: clientContext));
  196. await uploadMultipart(fileName,link);
  197. }
  198. }
  199. }
  200. cancel()async{
  201. http?.close();
  202. await dlulStream?.cancel();
  203. _progress?.close();
  204. }
  205. }