forked from omaha-consulting/omaha-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_delta.py
149 lines (128 loc) · 5.58 KB
/
new_delta.py
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
# Copyright (C) 2011 Crystalnix <[email protected]>
# This file is part of omaha-server.
# omaha-server 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.
# omaha-server 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 omaha-server. If not, see <http://www.gnu.org/licenses/>.
import re
from twisted.web import resource
from config import Config
from util import *
import hashlib, base64
class NewDeltaResource(resource.Resource):
isLeaf = True
pathFromRoot = '/service/admin/new_delta'
def render_GET(self, request):
output = """<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<title>House of Life Update Manager</title>
<!-- CSS: implied media="all" -->
<link rel="stylesheet" href="/css/style.css?v=2">
<link rel="stylesheet" href="/css/upload.css?v=2">
<script type="text/javascript">
uploadPath = "{0}";
</script>
<script type="text/javascript" src="/js/upload.js"></script>
</head>
<body>
<div id="container">
<header>
</header>
<div id="main" role="main">""".format(self.pathFromRoot)
output += """
<form id="form1" enctype="multipart/form-data" method="post" action="{0}">
<div class="row">
<label for="fromVersion">Input "From" Delta Update Version Number</label><br />
<input type="text" name="fromVersion" id="fromVersion" />
</div>
<div class="row">
<label for="fileToUpload">Select a File to Upload</label><br />
<input type="file" name="fileToUpload" id="fileToUpload" onchange="fileSelected();"/>
</div>
<div class="row">
<input type="button" onclick="uploadFile()" value="Upload" />
</div>
<div id="fileInfo">
<div id="fileName"></div>
<div id="fileSize"></div>
<div id="fileType"></div>
</div>
<div class="row"></div>
<div id="progressIndicator">
<div id="progressBar" class="floatLeft">
</div>
<div id="progressNumber" class="floatRight"> </div>
<div class="clear"></div>
<div>
<div id="transferSpeedInfo" class="floatLeft" style="width: 80px;"> </div>
<div id="timeRemainingInfo" class="floatLeft" style="margin-left: 10px;"> </div>
<div id="transferBytesInfo" class="floatRight" style="text-align: right;"> </div>
<div class="clear"></div>
</div>
<div id="uploadResponse"></div>
</div>
<p>
<a href="/service/admin">Back to main page</a>
</p>
</form>""".format(self.pathFromRoot)
output += """
</div>
<footer>
</footer>
</div>
</body>
</html>"""
return output
def render_POST(self, request):
"""
"""
versionRegex = re.compile('^\d+\.\d+\.\d+\.\d+$')
if not versionRegex.match(request.args['fromVersion'][0]):
request.setResponseCode(400) # Bad request
return "Error: malformed version number."
newDict = loadJsonAndCheckIfLatestKeyExists(Config.bitpopNewUpdateInfoFile)
if not newDict['latestExists']:
request.setResponseCode(500)
return 'Error: Bad new update information file or file does not exist.'
newInfo = newDict['jsonData']
outDir = os.path.join(Config.bitpopDirectory, newInfo['latest'])
if not os.path.exists(outDir):
request.setResponseCode(500)
return 'Error: Update directory not created yet.'
outDir = os.path.join(outDir, request.args['fromVersion'][0])
if not os.path.exists(outDir):
os.mkdir(outDir, 0755)
elif not os.path.isdir(outDir):
os.remove(outDir)
os.mkdir(outDir, 0755)
filename = os.path.join(outDir, Config.installerName)
try:
outputStream = open(filename, 'wb')
outputStream.write(request.args['fileToUpload'][0])
outputStream.close()
sha = hashlib.new('sha1')
sha.update(request.args['fileToUpload'][0])
hash = base64.b64encode(sha.digest())
if not newInfo.has_key('delta') or type(newInfo['delta']) != type([]):
newInfo['delta'] = []
if not newInfo.has_key('deltaHash') or type(newInfo['deltaHash']) != type([]):
newInfo['deltaHash'] = []
newInfo['delta'].append(request.args['fromVersion'][0])
newInfo['deltaHash'].append(hash)
bitpopUploadInfoFile = open(Config.bitpopNewUpdateInfoFile, 'w')
json.dump(newInfo, bitpopUploadInfoFile)
bitpopUploadInfoFile.close()
except:
request.setResponseCode(500) # Internal server error
return "Error: Internal Server Error. Failed to do some file operation."
return "OK. File was successfully uploaded to server."