• Welcome to the Chevereto user community!

    Here users from all over the world gather around to learn the latest about Chevereto and contribute with ideas to improve the software.

    Please keep in mind:

    • 😌 This community is user driven. Be polite with other users.
    • 👉 Is required to purchase a Chevereto license to participate in this community (doesn't apply to Pre-sales).
    • 💸 Purchase a Pro Subscription to get access to active software support and faster ticket response times.
  • Chevereto Support CLST

    Support response

    Support checklist

API Image Title

BigBoiJefe

Chevereto Member
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?
 
Create a custom API and inject the title via _REQUEST
 
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.
 
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']]);
 
Not the album, as you may notice it doesn't pass the album id in the $params array. I will have to modify that.
 
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.
 
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
 
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']]);
 
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.
 
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:
Back
Top