11#!/usr/bin/env python3
22# -*- coding:utf-8 -*-
3- # @Author: thepoy
4- # @Email: thepoy@163.com
5- # @File Name: imgtu.py
6- # @Created At: 2021-02-13 09:04:37
7- # @Modified At: 2023-04-19 14:48:44
8- # @Modified By: thepoy
93
104import os
115import re
126import time
137import json
14- import mimetypes
158import requests
169
1710from urllib import parse
2821 UploadErrorResponse ,
2922)
3023from up2b .up2b_lib .errors import MissingAuth
24+ from up2b .up2b_lib .file import File
3125from up2b .up2b_lib .http import upload_with_progress_bar
3226from up2b .up2b_lib .up2b_api import Base
3327from up2b .up2b_lib .constants import IMAGE_BEDS_NAME , ImageBedCode
@@ -53,8 +47,11 @@ def __init__(
5347 ignore_cache : bool = False ,
5448 conf : Optional [Config ] = None ,
5549 timeout : Optional [float ] = None ,
50+ quiet : bool = False ,
5651 ):
57- super ().__init__ (auto_compress , add_watermark , ignore_cache , conf , timeout )
52+ super ().__init__ (
53+ auto_compress , add_watermark , ignore_cache , conf , timeout , quiet
54+ )
5855
5956 self .cookie : Optional [str ] = None
6057 self .token : Optional [str ] = None
@@ -172,20 +169,10 @@ def __upload(
172169 else :
173170 filename = filename_with_suffix + "." + image .mime_type
174171
175- mime_type = (
176- mimetypes .guess_type (image )[0 ]
177- if isinstance (image , Path )
178- else image .mime_type
179- )
172+ file = File ("source" , image , filename = filename )
180173
181174 timestamp = int (time .time () * 1000 )
182175
183- if isinstance (image , Path ):
184- with open (image , "rb" ) as fb :
185- img_buffer = fb .read ()
186- else :
187- img_buffer = image .stream
188-
189176 data = {
190177 "type" : "file" ,
191178 "action" : "upload" ,
@@ -194,18 +181,18 @@ def __upload(
194181 "nsfw" : "0" ,
195182 }
196183
197- files = {
198- "source" : (filename , img_buffer , mime_type ),
199- }
200-
201184 try :
202185 if not self .quiet :
203186 resp = upload_with_progress_bar (
204- url , filename , dict ( data , ** files ) , self .headers
187+ url , file , self . timeout , data , self .headers
205188 )
206189 else :
207190 resp = requests .post (
208- url , headers = self .headers , data = data , files = files , timeout = self .timeout # type: ignore
191+ url ,
192+ headers = self .headers ,
193+ data = data ,
194+ files = file .to_dict (),
195+ timeout = self .timeout ,
209196 )
210197 except requests .exceptions .ConnectionError as e :
211198 return UploadErrorResponse (400 , str (e ), str (image ))
0 commit comments