Onionr/onionr/httpapi/miscpublicapi/upload.py

46 lines
1.6 KiB
Python
Raw Normal View History

2019-05-07 17:56:20 +00:00
'''
Onionr - Private P2P Communication
2019-05-07 17:56:20 +00:00
Accept block uploads to the public API server
'''
'''
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, see <https://www.gnu.org/licenses/>.
'''
import sys
from flask import Response, abort
import blockimporter, onionrexceptions, logger
2019-08-02 23:00:04 +00:00
def accept_upload(request):
2019-05-07 17:56:20 +00:00
resp = 'failure'
try:
data = request.form['block']
except KeyError:
logger.warn('No block specified for upload')
pass
else:
if sys.getsizeof(data) < 100000000:
try:
2019-07-19 04:59:44 +00:00
if blockimporter.importBlockFromData(data):
2019-05-07 17:56:20 +00:00
resp = 'success'
else:
resp = 'failure'
2019-05-07 17:56:20 +00:00
logger.warn('Error encountered importing uploaded block')
except onionrexceptions.BlacklistedBlock:
logger.debug('uploaded block is blacklisted')
resp = 'failure'
except onionrexceptions.DataExists:
resp = 'exists'
2019-05-07 17:56:20 +00:00
if resp == 'failure':
abort(400)
resp = Response(resp)
return resp