From af0f81832624cb953d74445c7a4c4c35ffb3fe06 Mon Sep 17 00:00:00 2001 From: j Date: Fri, 28 Jul 2023 20:45:31 +0200 Subject: [PATCH] fix chunk upload --- pandora_client/__init__.py | 50 +++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/pandora_client/__init__.py b/pandora_client/__init__.py index 5e72481..1eb3ecf 100755 --- a/pandora_client/__init__.py +++ b/pandora_client/__init__.py @@ -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)