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