ConverFilePlugin.swift 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //
  2. // ConverFilePlugin.swift
  3. // Runner
  4. //
  5. // Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. // This notice may not be removed from this file.
  11. //
  12. import Foundation
  13. import ComPDFKit_Conversion
  14. import Flutter
  15. class ConverFilePlugin{
  16. init(messenger : FlutterBinaryMessenger){
  17. let channel = FlutterEventChannel(name: "com.compdfkit.conversion.flutter.convert", binaryMessenger: messenger)
  18. channel.setStreamHandler(SwiftStreamHandler())
  19. }
  20. }
  21. class SwiftStreamHandler: NSObject,FlutterStreamHandler {
  22. var util:ConvertUtil?
  23. public func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
  24. let convertData = arguments as! [String : Any];
  25. let filePath = (convertData[params_file_path] as? String) ?? "";
  26. let convertType = convertData[params_convert_type] as! String;
  27. let taskId = (convertData[params_task_id] as? String) ?? "";
  28. let fileName = (convertData[params_file_name] as? String ) ?? "";
  29. let queue = DispatchQueue(label: "com.compdfkit.conversion.flutter")
  30. queue.async {
  31. self.util = ConvertUtil()
  32. let options = self.util?.getConvertOptions(withArguments: arguments)
  33. self.util?.convert(options: options!, filePath: filePath,fileName: fileName, convertType: convertType) { progress, status, outputPath in
  34. events(["progress": progress, params_task_id: taskId, "status" : status, "outputPath" : outputPath ?? ""] as [String : Any])
  35. }
  36. }
  37. return nil
  38. }
  39. public func onCancel(withArguments arguments: Any?) -> FlutterError? {
  40. return nil
  41. }
  42. }