When using API user upload + requests.post via python File too big..

Status
Not open for further replies.

neoark

Network license
Beta tester
Hello,

It seems like when I try to post an image via API it is using guest limits {"status_code":400,"error":{"message":"File too big - max 1 MB","code":313,"context":"CHV\\UploadException"},"status_txt":"Bad Request"} .

I am using the custom route with API user upload https://chevereto.com/docs/api-v1 .
// This will upload images to 'jesse' account
CHV\Image::uploadToWebsite($source, 'jesse');

Here is the sample code to post to API

Python:
#coding=utf-8
import requests
import json
import mimetypes
from PIL import ImageGrab
import datetime

def upload(files):
    APIKey = "YOUR API KEY"
    format = "json"
    url = "http://domain.com/api/1/upload/?key="+ APIKey + "&format=" + format
    #files =
    r = requests.post(url , files = files)

    return json.loads(r.text)

def formatSource(filename):
    imageList = []
    type = mimetypes.guess_type(filename)[0]
    imageList.append(('source' , (filename , open(filename , 'rb') , type)))
    print imageList
    return imageList

if __name__ == "__main__":
    print "将图片截图或复制到剪切板中即可~~, ctrl+z 结束"
    recentVal = None
    while(True):
        tmpValue = ImageGrab.grabclipboard()
        if recentVal != tmpValue:
            recentVal = tmpValue
            now = datetime.datetime.now()
            now = now.strftime("%Y-%m-%d %H:%M:%S")
            filename = 'IMG'+ now + '.png'
            if recentVal is not None:
                recentVal.save(filename, 'png')
                #filenames.append(filename)
                #recentVal = None
                print filename
                jsonData = upload(formatSource(filename))

                if jsonData['status_code'] != 200:
                    print "error: " , jsonData['error']['message']
                    print "status code : " , jsonData['status_code']
                else:
                    print "orignal url: " , jsonData['image']['display_url']
                    print "thumb url: " , jsonData['image']['thumb']['url']
 

neoark

Network license
Beta tester
Of course it uses guest limits, the API is not user based.
So when you specify CHV\Image::uploadToWebsite($source, 'jesse'); it uploads as users and uses guest limits? Would it be possible to use registered user limit if a username is specified in custom routes?
 

Rodolfo

Chevereto Developer
Chevereto Staff
This is because app/loader.php does this:

PHP:
            // Guest upload limit, sets local value
            if(!Login::getUser() && $upload_allowed && getSetting('upload_max_filesize_mb_guest')) {
                Settings::setValue('upload_max_filesize_mb_bak', getSetting('upload_max_filesize_mb'));
                Settings::setValue('upload_max_filesize_mb', getSetting('upload_max_filesize_mb_guest'));
            }
app/loader.php loads before the API, so I will have to add another workaround to fool this limit when you force the username in the method you described above.

This will get patched but please note that I'm working on V4 and I won't issue this patch anytime soon. Please bear the fact that my goal is V4 and that every little bug in V3 API is meaningless to V4 because V4 API is completely different.
 

neoark

Network license
Beta tester
Thanks. I have increased the limit to get around it. Luckily it's not using the expiration_date set on the guest uploads.
 

Rodolfo

Chevereto Developer
Chevereto Staff
app/lib/classes/class.image.php

add this:

PHP:
                if($user !== NULL && getSetting('upload_max_filesize_mb') == getSetting('upload_max_filesize_mb_guest')) {
                    Settings::setValue('upload_max_filesize_mb', getSetting('upload_max_filesize_mb'));
                }
Just below this:
PHP:
            // Get user
            if($user) {
                switch(gettype($user)) {
                    case 'string':
                        $user = User::getSingle($user, 'username');
                    break;
                    case 'integer':
                        $user = User::getSingle($user);
                    break;
                }
Try it and please let me know if works for you.
 
Status
Not open for further replies.
Top