Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

1adcb0fe-6fe8-4f54-b5a0-9a7c5cc2e527 2.7 KB
Raw

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  1. from pubnub.endpoints.file_operations.file_based_endpoint import FileOperationEndpoint
  2. from pubnub.enums import HttpMethod, PNOperationType
  3. from pubnub.crypto import PubNubFileCrypto
  4. from pubnub.models.consumer.file import PNDownloadFileResult
  5. from pubnub.request_handlers.requests_handler import RequestsRequestHandler
  6. from pubnub.endpoints.file_operations.get_file_url import GetFileDownloadUrl
  7. from warnings import warn
  8. class DownloadFileNative(FileOperationEndpoint):
  9. def __init__(self, pubnub):
  10. FileOperationEndpoint.__init__(self, pubnub)
  11. self._file_id = None
  12. self._file_name = None
  13. self._pubnub = pubnub
  14. self._download_data = None
  15. self._cipher_key = None
  16. def cipher_key(self, cipher_key):
  17. warn('Deprecated: Usage of local cipher_keys is discouraged. Use pnconfiguration.cipher_key instead')
  18. self._cipher_key = cipher_key
  19. return self
  20. def build_path(self):
  21. return self._download_data.result.file_url
  22. def http_method(self):
  23. return HttpMethod.GET
  24. def is_auth_required(self):
  25. return False
  26. def custom_params(self):
  27. return {}
  28. def file_id(self, file_id):
  29. self._file_id = file_id
  30. return self
  31. def file_name(self, file_name):
  32. self._file_name = file_name
  33. return self
  34. def decrypt_payload(self, data):
  35. if self._cipher_key:
  36. return PubNubFileCrypto(self._pubnub.config).decrypt(self._cipher_key, data)
  37. else:
  38. return self._pubnub.crypto.decrypt_file(data)
  39. def validate_params(self):
  40. self.validate_subscribe_key()
  41. self.validate_channel()
  42. self.validate_file_name()
  43. self.validate_file_id()
  44. def create_response(self, envelope):
  45. if self._cipher_key or self._pubnub.config.cipher_key:
  46. return PNDownloadFileResult(self.decrypt_payload(envelope.content))
  47. else:
  48. return PNDownloadFileResult(envelope.content)
  49. def non_json_response(self):
  50. return True
  51. def operation_type(self):
  52. return PNOperationType.PNDownloadFileAction
  53. def use_base_path(self):
  54. return False
  55. def build_params_callback(self):
  56. return lambda a: {}
  57. def name(self):
  58. return "Downloading file"
  59. def sync(self):
  60. self._download_data = GetFileDownloadUrl(self._pubnub)\
  61. .channel(self._channel)\
  62. .file_name(self._file_name)\
  63. .file_id(self._file_id)\
  64. .sync()
  65. return super(DownloadFileNative, self).sync()
  66. def pn_async(self, callback):
  67. return RequestsRequestHandler(self._pubnub).async_file_based_operation(self.sync, callback, "File Download")
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...