Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UploadSrImg Issues #118

Open
Yay295 opened this issue Nov 23, 2024 · 0 comments
Open

UploadSrImg Issues #118

Yay295 opened this issue Nov 23, 2024 · 0 comments

Comments

@Yay295
Copy link

Yay295 commented Nov 23, 2024

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()
    );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant