devvit icon indicating copy to clipboard operation
devvit copied to clipboard

UploadSrImg Issues

Open Yay295 opened this issue 1 year ago • 0 comments

The documentation on UploadSrImgRequest is wrong, and the header field is redundant/obsolete. It should look like this:

export interface UploadSrImgRequest {
    /** file upload with maximum size of 500 KiB */
    file: string;
    /** one of png or jpg (default: png) */
    imgType: 'png' | 'jpg';
    /** a valid subreddit image name */
    name: string;
    /* one of (img, header, icon, banner) */
    uploadType: 'img' | 'header' | 'icon' | 'banner';
    /** the name of the subreddit */
    subreddit: string;
}

I'm currently getting an error fetching the image to upload (HTTP request to domain: styles.redditmedia.com timed out with error: context deadline exceeded), but previously I've gotten a different error:

{
  errors: [ 'IMAGE_ERROR' ],
  imgSrc: '',
  errorsValues: [ 'Invalid image or general image error' ]
}

I assume this is due to the encoding of the image data, but I'm not sure if it's because of my code, or because the upload code is writing the image data as a string instead of bytes (@devvit/protos/types/devvit/plugin/redditapi/subreddits/subreddits_msg.js line 1854). If it should be writing the data as bytes, then the file field in UploadSrImgRequest should have the type Uint8Array | string.

Test code:

import { Devvit } from '@devvit/public-api';
import { getMetadata } from '@devvit/runtimes/plugins/helpers.js';
import { Subreddits } from '@devvit/runtimes/plugins/redditapi/Subreddits.js';

async function testUploadSrImg(imageUrl: string, imageName: string, subredditName: string) {
    const imageType = imageUrl.slice(-3);
    if (imageType != 'png' && imageType != 'jpg') {
        return;
    }
    const imageData = await fetch(imageUrl).then(response => {
        return response.ok ? response.arrayBuffer() : null;
    });
    if (!imageData) {
        return;
    }
    await ((Devvit as any).redditAPIPlugins.Subreddits as Subreddits).UploadSrImg(
        {
            file: Buffer.from(imageData).toString('binary'),
            header: 0,
            imgType: imageType,
            name: imageName,
            uploadType: 'img',
            subreddit: subredditName
        },
        getMetadata()
    );
}

Yay295 avatar Nov 23 '24 22:11 Yay295