Mercurial > libervia-backend
comparison sat/plugins/plugin_misc_android.py @ 2868:5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
seek() is bugged on current version of P4A, arguments are inverted.
More details can be found at https://github.com/kivy/python-for-android/issues/1768
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 25 Mar 2019 07:06:41 +0100 |
parents | 26d6ac4e4a66 |
children | 6b00f88316bf |
comparison
equal
deleted
inserted
replaced
2867:3cac3d050046 | 2868:5546613f5007 |
---|---|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | 19 |
20 import sys | 20 import sys |
21 import os | 21 import os |
22 import os.path | 22 import os.path |
23 import tempfile | |
23 from sat.core.i18n import _, D_ | 24 from sat.core.i18n import _, D_ |
24 from sat.core.constants import Const as C | 25 from sat.core.constants import Const as C |
25 from sat.core.log import getLogger | 26 from sat.core.log import getLogger |
26 from sat.core import exceptions | 27 from sat.core import exceptions |
27 from twisted.internet import reactor | 28 from twisted.internet import reactor |
28 from twisted.internet import protocol | 29 from twisted.internet import protocol |
29 from twisted.internet import error as int_error | 30 from twisted.internet import error as int_error |
31 from twisted.web import client as web_client | |
30 | 32 |
31 log = getLogger(__name__) | 33 log = getLogger(__name__) |
32 | 34 |
33 PLUGIN_INFO = { | 35 PLUGIN_INFO = { |
34 C.PI_NAME: "Android ", | 36 C.PI_NAME: "Android ", |
53 SOCKET_FILE = ".socket" | 55 SOCKET_FILE = ".socket" |
54 STATE_RUNNING = "running" | 56 STATE_RUNNING = "running" |
55 STATE_PAUSED = "paused" | 57 STATE_PAUSED = "paused" |
56 STATE_STOPPED = "stopped" | 58 STATE_STOPPED = "stopped" |
57 STATES = (STATE_RUNNING, STATE_PAUSED, STATE_STOPPED) | 59 STATES = (STATE_RUNNING, STATE_PAUSED, STATE_STOPPED) |
60 | |
61 | |
62 def determineLength_workaround(self, fObj): | |
63 """Method working around seek() bug on Android""" | |
64 try: | |
65 seek = fObj.seek | |
66 tell = fObj.tell | |
67 except AttributeError: | |
68 return web_client.UNKNOWN_LENGTH | |
69 originalPosition = tell() | |
70 seek(os.SEEK_END) | |
71 end = tell() | |
72 seek(os.SEEK_SET, originalPosition) | |
73 return end - originalPosition | |
74 | |
75 | |
76 def patch_seek_bug(): | |
77 """Check seek bug and apply a workaround if still here | |
78 | |
79 cf. https://github.com/kivy/python-for-android/issues/1768 | |
80 """ | |
81 with tempfile.TemporaryFile() as f: | |
82 f.write(b'1234567890') | |
83 f.seek(0, os.SEEK_END) | |
84 size = f.tell() | |
85 if size == 10: | |
86 log.info(u"seek() bug not present anymore, workaround code can be removed") | |
87 else: | |
88 log.warning(u"seek() bug detected, applying a workaround") | |
89 web_client.FileBodyProducer._determineLength = determineLength_workaround | |
90 | |
91 patch_seek_bug() | |
58 | 92 |
59 | 93 |
60 class FrontendStateProtocol(protocol.Protocol): | 94 class FrontendStateProtocol(protocol.Protocol): |
61 | 95 |
62 def __init__(self, android_plugin): | 96 def __init__(self, android_plugin): |