not-an-aardvark / snoowrap

A JavaScript wrapper for the reddit API
MIT License
1.01k stars 125 forks source link

Upload image to Reddit native uploader #280

Open joey-kwl opened 3 years ago

joey-kwl commented 3 years ago

If I want to upload an image I have to upload via Imgur or other images hosting, but Reddit have their own images hosting platform:

Image of Yaktocat

iMrDJAi commented 3 years ago

I think Reddit does not allow that

iMrDJAi commented 3 years ago

Edit: It's more than possible, in fact PRAW does support that, it's just that the API endpoints used to upload media are undocumented.

That's how I did it:

async function uploadMedia(filename, mimetype, blob) {
    const uploadResponse = await requester.oauthRequest({
        uri: "api/media/asset.json",
        method: "post",
        form: {
            filepath: filename,
            mimetype
        }
    })
    const uploadURL = "https:" + uploadResponse.args.action
    const formdata = new FormData()
    uploadResponse.args.fields.forEach(item => formdata.append(item.name, item.value))
    formdata.append('file', blob, filename)
    const response = await fetch(uploadURL, {
        method: "post",
        mode: "no-cors",
        body: formdata
    })    
    return {
      asset_id: uploadResponse.asset.asset_id,
      link: uploadURL + "/" + uploadResponse.args.fields.find(item => item.name === "key").value,
      websocket_url: uploadResponse.asset.websocket_url
    }
}

fetch("https://i.imgur.com/LYcDAGm.png").then(async res => {
    const blob = await res.blob()
    console.log(await uploadMedia("LYcDAGm.png", "image/png", blob))
})
Venefilyn commented 3 years ago

Any undocumented endpoints are subject to change at any moment as Reddit will not have a deprecation policy for them. I'm skeptical about including undocumented endpoints as part of Snoowrap itself

VityaSchel commented 2 years ago

Any undocumented endpoints are subject to change at any moment as Reddit will not have a deprecation policy for them. I'm skeptical about including undocumented endpoints as part of Snoowrap itself

then I suppose we should remove "full featured" from wrapper description, because basically none of reddit api wrappers for js can submit images, though most of them calls itself full-featured

iMrDJAi commented 2 years ago

@SpyTec The official API docs aren't a reliable source anymore since they haven't got updated for a while. I think snoowrap should really consider switching to support all known endpoints instead of documented endpoints only.

ProdigyView commented 11 months ago

Edit: It's more than possible, in fact PRAW does support that, it's just that the API endpoints used to upload media are undocumented.

That's how I did it:

async function uploadMedia(filename, mimetype, blob) {
    const uploadResponse = await requester.oauthRequest({
        uri: "api/media/asset.json",
        method: "post",
        form: {
            filepath: filename,
            mimetype
        }
    })
    const uploadURL = "https:" + uploadResponse.args.action
    const formdata = new FormData()
    uploadResponse.args.fields.forEach(item => formdata.append(item.name, item.value))
    formdata.append('file', blob, filename)
    const response = await fetch(uploadURL, {
        method: "post",
        mode: "no-cors",
        body: formdata
    })    
    return {
      asset_id: uploadResponse.asset.asset_id,
      link: uploadURL + "/" + uploadResponse.args.fields.find(item => item.name === "key").value,
      websocket_url: uploadResponse.asset.websocket_url
    }
}

fetch("https://i.imgur.com/LYcDAGm.png").then(async res => {
    const blob = await res.blob()
    console.log(await uploadMedia("LYcDAGm.png", "image/png", blob))
})

This still works. Should be incorporated in the library,