fix chunk upload

This commit is contained in:
j 2023-07-28 20:45:31 +02:00
parent fb174c990e
commit af0f818326

View file

@ -8,6 +8,7 @@ from glob import glob
import importlib.util
import json
import math
import mimetypes
import os
import socket
import sqlite3
@ -1045,16 +1046,21 @@ class API(ox.API):
def uploadFrames(self, i, data):
# upload frames
if self.site['media'].get('importFrames') and i['frames']:
form = ox.MultiPartForm()
form.add_field('action', 'upload')
form.add_field('id', i['oshash'])
form = {
'action': 'upload',
'id': i['oshash']
}
files = []
for key in data:
form.add_field(key, data[key])
form[key] = data[key]
for frame in i['frames']:
fname = os.path.basename(frame)
if os.path.exists(frame):
form.add_file('frame', fname, open(frame, 'rb'))
r = self._json_request(self.url, form)
mimetype = mimetypes.guess_type(frame)[0] or 'application/octet-stream'
files.append(
('frame', (fname, open(frame, 'rb'), mimetype))
)
r = self._json_request(self.url, form, files)
return r
def uploadVideo(self, filename, data, profile, info=None):
@ -1085,18 +1091,21 @@ class API(ox.API):
def uploadData(self, filename, oshash):
if DEBUG:
print('upload', filename)
form = ox.MultiPartForm()
form.add_field('action', 'upload')
form.add_field('id', str(oshash))
form = {
'action': 'upload',
'id': str(oshash)
}
fname = os.path.basename(filename)
if not isinstance(fname, bytes):
fname = fname.encode('utf-8')
form.add_file('file', fname, open(filename, 'rb'))
r = self._json_request(self.url, form)
mimetype = mimetypes.guess_type(filename)[0] or 'application/octet-stream'
files = [
('file', (fname, open(filename, 'rb'), mimetype))
]
r = self._json_request(self.url, form, files)
return r
def upload_chunks(self, url, filename, data=None):
form = ox.MultiPartForm()
resume = None
if self._resume_file and os.path.exists(self._resume_file):
with open(self._resume_file) as f:
@ -1109,9 +1118,7 @@ class API(ox.API):
if resume:
data = resume
else:
for key in data:
form.add_field(key, data[key])
data = self._json_request(url, form)
data = self._json_request(url, data)
print(filename)
hide_cursor()
@ -1137,6 +1144,7 @@ class API(ox.API):
resume_offset = 0
chunk = f.read(CHUNK_SIZE)
fname = os.path.basename(filename)
mimetype = mimetypes.guess_type(filename)[0] or 'application/octet-stream'
if not isinstance(fname, bytes):
fname = fname.encode('utf-8')
while chunk:
@ -1151,13 +1159,15 @@ class API(ox.API):
100 * done/fsize, ox.format_bytes(done), ox.format_bytes(fsize), remaining)
print(''.join([msg, ' ' * (80-len(msg)), '\r']), end='')
sys.stdout.flush()
form = ox.MultiPartForm()
form.add_file('chunk', fname, chunk)
form = {}
if len(chunk) < CHUNK_SIZE or f.tell() == fsize:
form.add_field('done', '1')
form.add_field('offset', str(done))
form['done'] = '1'
form['offset'] = str(done)
files = [
('file', (fname, chunk, mimetype))
]
try:
data = self._json_request(uploadUrl, form)
data = self._json_request(uploadUrl, form, files)
except KeyboardInterrupt:
print("\ninterrupted by user.")
sys.exit(1)