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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
# Copyright 2013 Agustin Zubiaga <aguz@sugarlabs.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
from gi.repository import GObject
import base64
import os
import json
import dbus
from zipfile import ZipFile
import logging
import websocket
import tempfile
from sugar3 import profile
CHUNK_SIZE = 2048
class Uploader(GObject.GObject):
__gsignals__ = {'uploaded': (GObject.SignalFlags.RUN_FIRST, None, ([]))}
def __init__(self, file_path, url):
GObject.GObject.__init__(self)
logging.error('websocket url %s', url)
# base64 encode the file
self._file = tempfile.TemporaryFile(mode='r+')
base64.encode(open(file_path, 'r'), self._file)
self._file.seek(0)
self._ws = websocket.WebSocketApp(url,
on_open=self._on_open,
on_message=self._on_message,
on_error=self._on_error,
on_close=self._on_close)
self._chunk = str(self._file.read(CHUNK_SIZE))
def start(self):
from threading import Thread
upload_looop = Thread(target=self._ws.run_forever)
upload_looop.setDaemon(True)
upload_looop.start()
def _on_open(self, ws):
if self._chunk != '':
self._ws.send(self._chunk)
else:
self._ws.close()
def _on_message(self, ws, message):
self._chunk = self._file.read(CHUNK_SIZE)
if self._chunk != '':
self._ws.send(self._chunk)
else:
self._ws.close()
def _on_error(self, ws, error):
#self._ws.send(self._chunk)
pass
def _on_close(self, ws):
self._file.close()
GObject.idle_add(self.emit, 'uploaded')
def get_user_data():
"""
Create this structure:
{"from": "Walter Bender", "icon": ["#FFC169", "#FF2B34"]}
used to identify the owner of a shared object
is compatible with how the comments are saved in
http://wiki.sugarlabs.org/go/Features/Comment_box_in_journal_detail_view
"""
xo_color = profile.get_color()
data = {}
data['from'] = profile.get_nick_name()
data['icon'] = [xo_color.get_stroke_color(), xo_color.get_fill_color()]
return data
def package_ds_object(dsobj, destination_path):
"""
Creates a zipped file with the file associated to a journal object,
the preview and the metadata
"""
object_id = dsobj.object_id
logging.error('id %s', object_id)
preview_path = None
logging.error('before preview')
if 'preview' in dsobj.metadata:
# TODO: copied from expandedentry.py
# is needed because record is saving the preview encoded
if dsobj.metadata['preview'][1:4] == 'PNG':
preview = dsobj.metadata['preview']
else:
# TODO: We are close to be able to drop this.
preview = base64.b64decode(dsobj.metadata['preview'])
preview_path = os.path.join(destination_path,
'preview_id_' + object_id)
preview_file = open(preview_path, 'w')
preview_file.write(preview)
preview_file.close()
logging.error('before metadata')
# create file with the metadata
metadata_path = os.path.join(destination_path,
'metadata_id_' + object_id)
metadata_file = open(metadata_path, 'w')
metadata = {}
for key in dsobj.metadata.keys():
if key not in ('object_id', 'preview', 'progress'):
metadata[key] = dsobj.metadata[key]
metadata_file.write(json.dumps(metadata))
metadata_file.close()
logging.error('before create zip')
# create a zip fileincluding metadata and preview
# to be read from the web server
file_path = os.path.join(destination_path, 'id_' + object_id + '.journal')
with ZipFile(file_path, 'w') as myzip:
if preview_path is not None:
myzip.write(preview_path, 'preview')
myzip.write(metadata_path, 'metadata')
myzip.write(dsobj.file_path, 'data')
return file_path
def unpackage_ds_object(origin_path, dsobj=None):
"""
Receive a path of a zipped file, unzip it, and save the data,
preview and metadata on a journal object
"""
tmp_path = os.path.dirname(origin_path)
with ZipFile(origin_path) as zipped:
metadata = json.loads(zipped.read('metadata'))
preview_data = zipped.read('preview')
zipped.extract('data', tmp_path)
if dsobj is not None:
for key in metadata.keys():
dsobj.metadata[key] = metadata[key]
dsobj.metadata['preview'] = dbus.ByteArray(preview_data)
dsobj.file_path = os.path.join(tmp_path, 'data')
else:
return metadata, preview_data, os.path.join(tmp_path, 'data')
|