UploadSrImg Issues

Open
#118 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
typescript
Domain
api, documentation

Research direction

Start with the UploadSrImgRequest definition and @devvit/protos/types/devvit/plugin/redditapi/subreddits/subreddits_msg.js around line 1854, then compare them with the supplied testUploadSrImg example. Reproduce the upload error if possible and verify how the image payload is represented. Done means the documented interface matches the implementation and the file encoding behavior is established or corrected.

Written by the indexing model from the issue text.

Description

reddit-api

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()
    );
}
Dominant language
TypeScript
Stars
210
Forks
88
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from reddit/devvit

All issues in reddit/devvit

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.