• Purchasing a license grants permission to use the software and to get Tech Support. A license purchase doesn't grant customization support.

API Image Title

BigBoiJefe

Network license
Beta tester
Hi, i'm using the api, however, I need to know if there's a way to pass a title of the uploaded image? If this does not exist, how much to add this?
 

Rodolfo

Chevereto Developer
Chevereto Staff
Like I said, is dead easy. How you want me to ask money for change one line?

In route.api.php this is the function that uploads the content to the website:

PHP:
$uploaded_id = CHV\Image::uploadToWebsite($source, NULL, NULL);

And if you dig the code in class.image.php you will notice which params this function accepts:
PHP:
public static function uploadToWebsite($source, $user, $params=[]) {

The third param ($params=[]) is an array which may contain additional data, if you scroll down a little bit you will see:
PHP:
            $image_insert_values = [
                'storage_mode'    => $storage_mode,
                'storage_id'    => $storage_id, // nota: real storage id
                'user_id'        => $user['id'],
                'album_id'        => NULL,
                'nsfw'            => $params['nsfw'],
                'category_id'    => $params['category_id'],
                'title'            => $params['title'],
                'description'    => $params['description'],
                'chain'            => $chain_value,
                'thumb_size'    => $image_thumb['fileinfo']['size'],
                'medium_size'    => $image_medium['fileinfo']['size'] ?: 0      
            ];
As you may notice, a lot of infomation can be injected just populating the $params array.


So this line in route.api.php:
PHP:
$uploaded_id = CHV\Image::uploadToWebsite($source, NULL, NULL);

Shoud be something like this:
PHP:
$uploaded_id = CHV\Image::uploadToWebsite($source, NULL, ['title' => $_REQUEST['title']]);

Where $_REQUEST['title'] comes from the request. And done, the title can be injected using the API.
 

BigBoiJefe

Network license
Beta tester
Can I then add the user_id, category_id and album_id as well to the same request?

PHP:
$uploaded_id = CHV\Image::uploadToWebsite($source, NULL, ['title' => $_REQUEST['title']]['album_id' => $_REQUEST['album_id']]['category_id' => $_REQUEST['category_id']]['user_id' => $_REQUEST['id']]);
 

Rodolfo

Chevereto Developer
Chevereto Staff
Not the album, as you may notice it doesn't pass the album id in the $params array. I will have to modify that.
 

Rodolfo

Chevereto Developer
Chevereto Staff
Actually is easier than that... Check the function:

PHP:
    public static function uploadToWebsite($source, $user, $params=[]) {
       
        try {
           
            if($user) {
                switch(gettype($user)) {
                    case 'string':
                        $user = User::getSingle($user, 'username');
                    break;
                    case 'integer':
                        $user = User::getSingle($user);
                    break;
                }
            }
So you can pass $user directly as username or user id.
 

BigBoiJefe

Network license
Beta tester
I'm missing something, if I use

PHP:
$uploaded_id = CHV\Image::uploadToWebsite($source,NULL,['title' => $_REQUEST['title']],['category_id' => $_REQUEST['category_id']]);
Then when I pass &title=blah&category_id=1 the actual status result shows "category_id":null
 

Rodolfo

Chevereto Developer
Chevereto Staff
Your code is wrong. You are passing just the title in the $param array.

PHP:
$uploaded_id = CHV\Image::uploadToWebsite($source,NULL,['title' => $_REQUEST['title'],'category_id' => $_REQUEST['category_id']]);
 

BigBoiJefe

Network license
Beta tester
Yes. You can pass anything. Just check the code and notice that the user_id must be passed as the second parameter of the function.
I can see that if I change NULL to 'Username', it does in fact upload to that username, however, I'm not looking to modify the php file everytime I need to change this.
 

Rodolfo

Chevereto Developer
Chevereto Staff
Yes it was added and if you want to customize the api you should use the route override system. With that thing you don't need to worry about code changes at route level. Always read the manual before doing this kind of stuff. https://chevereto.com/docs/routes
 
Last edited:
Top