Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Storing Uint8Array data to IPFS changes data when getting the same data #1282

Closed
moda20 opened this issue Mar 27, 2018 · 4 comments
Closed
Labels
kind/support A question or request for support

Comments

@moda20
Copy link

moda20 commented Mar 27, 2018

  • Version: 0.27.7
  • Platform: Linux
  • Subsystem: Archlinux Zen Kernel

Type:

Problem

Severity:

High

Description:

I am trying to store an image to IPFS from my nodejs Server, The image is represented with an Uint8Array sent from my angular5 Client so basically :
WebClient (angular5) => NodeJs(Express) => IPFS node

When sending data form my web client i can see that i am sending an Uint8Array of 4000 items
Uint8Array[4000] . When getting it back from IPFS and then sending it to my Web client i find out an Uint8Array of more than 4000 mostly double the number of items ( something like Uint8Array[8000]) and it doesn't translate to an image when reading it afterwards ( using FileReader)
This is my code :

This is the image reading function

const reader = new FileReader();
            reader.onload = (e) => {
                let file=reader.result;
                let buff = new Buffer(file);
                that.CustomerInfo["KImage"] = buff;
                console.log("read the file into buffer", that.CustomerInfo)
                that.ConfirmButtonDisabled=false;
            }
            reader.readAsBinaryString(NativeInput.prop('files')[0]);

With NativeInput the Jquery input item.

this is the storing function from my nodeJs Server :

    return new Promise(function (resolve, reject) {
        try{

            Localipfs.files.add(url, function(err, result) {
                if (err) {
                    reject("Content submission error:", err);
                    console.error("Content submission error:", err);
                } else if (result && result[0] && result[0].hash) {
                    console.log("Content successfully stored. IPFS address:", result[0].hash);
                    resolve(result[0].hash);
                } else {
                    reject("Unresolved content submission error");
                    console.log(result);
                    console.error("Unresolved content submission error");
                }
            });
        }catch (e){
            console.error("error from storing content"+e);
        }
    })
}

the url argument is a nodeJs Buffer from the Uint8Array sent from my webClient.

this is the data getting function from IPFS

function getContent(hash){
    return new Promise((res,rej)=>{
        LIPFS.object.data(hash).then(
            data=>{
                console.log(JSON.stringify(data));
                        res(data);
            }
        ).catch(
            err=>{
                rej(err);
            }
        )
    })
 
}

When logging the stringifyed data, i get a json object with type: Buffer and a data field which is an array of Uint8 but it turns out that is not the data i stored.

I searched elsewhere and i am not sure between a bug/problem with my IPFS or a problem of conversion between NodeJs Buffers and Web Buffers.

@travisperson
Copy link
Member

Hey @moda20,

You are going to want to use the the files.cat API to request the content added through files.add.

The object.get method returns the raw object. In this case it's the root object of the unixfs MerkleDAG.

@daviddias daviddias added the kind/support A question or request for support label Jun 4, 2018
@daviddias
Copy link
Member

@travisperson is correct. Closing this issue. @moda20 let us know if the solution @travisperson worked for you, if not, we reopen again :)

@RishabhS7
Copy link

I am using "ipfs-http-client" . I tried ".cat" with valid cid , but it just just gives suspended in console.
I am uploading encrypted file . But if i am not getting same uint8array , then it is not going to get decrypted.
Please let me know a proper solution to this.

@achingbrain
Copy link
Member

@RishabhS7 can you please ask your question on https://discuss.ipfs.io in the js-ipfs category, and include the code you are trying to run.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind/support A question or request for support
Projects
None yet
Development

No branches or pull requests

5 participants