2016-05-07 12:26:40 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-11-22 13:35:19 -06:00
|
|
|
from __future__ import absolute_import
|
2019-06-06 11:52:23 -05:00
|
|
|
from __future__ import division
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
from builtins import object
|
|
|
|
from past.utils import old_div
|
2016-05-07 12:26:40 -05:00
|
|
|
import sys
|
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
import logging
|
2018-11-22 13:35:19 -06:00
|
|
|
from .utils import convert_bytes
|
2016-05-07 12:26:40 -05:00
|
|
|
from pubsub import pub
|
|
|
|
log = logging.getLogger("extra.AudioUploader.transfer")
|
|
|
|
from requests_toolbelt.multipart.encoder import MultipartEncoder, MultipartEncoderMonitor
|
|
|
|
import requests
|
|
|
|
import os
|
|
|
|
class Upload(object):
|
|
|
|
def __init__(self, field=None, obj=None, url=None, filename=None, follow_location=True, completed_callback=None, verbose=False, *args, **kwargs):
|
|
|
|
super(Upload, self).__init__(*args, **kwargs)
|
|
|
|
self.url=url
|
|
|
|
self.filename=filename
|
|
|
|
log.debug("Uploading audio to %s, filename %s" % (url, filename))
|
|
|
|
self.start_time = None
|
|
|
|
self.completed_callback = completed_callback
|
|
|
|
self.background_thread = None
|
|
|
|
self.transfer_rate = 0
|
2016-07-27 12:05:35 -05:00
|
|
|
self.local_filename=os.path.basename(self.filename)
|
2019-06-06 11:52:23 -05:00
|
|
|
if isinstance(self.local_filename, str):
|
2016-07-27 12:05:35 -05:00
|
|
|
self.local_filename=self.local_filename.encode(sys.getfilesystemencoding())
|
2017-05-02 15:55:43 -05:00
|
|
|
self.fin=open(self.filename, 'rb')
|
|
|
|
self.m = MultipartEncoder(fields={field:(self.local_filename, self.fin, "application/octet-stream")})
|
2016-05-07 12:26:40 -05:00
|
|
|
self.monitor = MultipartEncoderMonitor(self.m, self.progress_callback)
|
|
|
|
self.response=None
|
|
|
|
self.obj=obj
|
|
|
|
self.follow_location=follow_location
|
|
|
|
#the verbose parameter is deprecated and will be removed soon
|
|
|
|
|
|
|
|
def elapsed_time(self):
|
|
|
|
if not self.start_time:
|
|
|
|
return 0
|
|
|
|
return time.time() - self.start_time
|
|
|
|
|
|
|
|
def progress_callback(self, monitor):
|
|
|
|
progress = {}
|
|
|
|
progress["total"] = monitor.len
|
|
|
|
progress["current"] = monitor.bytes_read
|
|
|
|
if progress["current"] == 0:
|
|
|
|
progress["percent"] = 0
|
|
|
|
self.transfer_rate = 0
|
|
|
|
else:
|
|
|
|
progress["percent"] = int((float(progress["current"]) / progress["total"]) * 100)
|
2019-06-06 11:52:23 -05:00
|
|
|
self.transfer_rate = old_div(progress["current"], self.elapsed_time())
|
2016-05-07 12:26:40 -05:00
|
|
|
progress["speed"] = '%s/s' % convert_bytes(self.transfer_rate)
|
|
|
|
if self.transfer_rate:
|
2019-06-06 11:52:23 -05:00
|
|
|
progress["eta"] = old_div((progress["total"] - progress["current"]), self.transfer_rate)
|
2016-05-07 12:26:40 -05:00
|
|
|
else:
|
|
|
|
progress["eta"] = 0
|
|
|
|
pub.sendMessage("uploading", data=progress)
|
|
|
|
|
|
|
|
def perform_transfer(self):
|
|
|
|
log.debug("starting upload...")
|
|
|
|
self.start_time = time.time()
|
|
|
|
self.response=requests.post(url=self.url, data=self.monitor, headers={"Content-Type":self.m.content_type}, allow_redirects=self.follow_location, stream=True)
|
|
|
|
log.debug("Upload finished.")
|
|
|
|
self.complete_transfer()
|
|
|
|
|
|
|
|
def perform_threaded(self, *args, **kwargs):
|
|
|
|
self.background_thread = threading.Thread(target=self.perform_transfer)
|
|
|
|
self.background_thread.daemon = True
|
|
|
|
self.background_thread.start()
|
|
|
|
|
|
|
|
def complete_transfer(self):
|
|
|
|
if callable(self.completed_callback):
|
|
|
|
self.completed_callback(self.obj)
|
2017-05-02 15:55:43 -05:00
|
|
|
if hasattr(self,'fin') and callable(self.fin.close):
|
|
|
|
self.fin.close()
|
2018-01-31 17:12:43 -06:00
|
|
|
|
2016-05-07 12:26:40 -05:00
|
|
|
def get_url(self):
|
2018-01-31 17:12:43 -06:00
|
|
|
try:
|
|
|
|
data = self.response.json()
|
|
|
|
except:
|
|
|
|
return _("Error in file upload: {0}").format(self.data.content,)
|
2018-11-22 13:35:19 -06:00
|
|
|
if "url" in data and data["url"] != "0":
|
2018-01-31 17:12:43 -06:00
|
|
|
return data["url"]
|
2018-11-22 13:35:19 -06:00
|
|
|
elif "error" in data and data["error"] != "0":
|
2018-01-31 17:12:43 -06:00
|
|
|
return data["error"]
|
|
|
|
else:
|
|
|
|
return _("Error in file upload: {0}").format(self.data.content,)
|