Loading a file into the STEEM blockchain

in #utopian-io6 years ago (edited)

A few weeks ago, the question came up:

Is it possible to get files into the STEEM blockchain?

As I have discovered, with a few little tricks you can get everything into the STEEM blockchain.

The following scripts are for testing purposes and not for productive use

I wrote this tutorial for @pcsg-dev, but I don't want to post it with this account for a variety of reasons. I also posted an issue for Steemit and waited some time until it could be reacted to, but it is not taken seriously.

bandbreite-computer-daten-1148820.jpg

What is needed

  • A STEEM account
  • One file
  • Some know-how in programming
    • Our example scripts are written in PHP

I tried to make the tutorial as simple as possible.
However, some prerequisites are necessary, so I classify this for experienced users.

What will you learn:

  • How to split files with PHP
  • How can you upload files to the STEEM blockchain
  • How can you reassemble split files with PHP

Difficulty

  • Intermediate

General procedure

  • We split our file into small pieces
    • This is because STEEM has a maximum length of a post
  • We convert each section into text
    • This is because we get binary data only badly in a post
  • We write a post / comment for each section
    • The content of a post is the content of a file
  • We read every single post and put all content back together again

Tutorial

  • All examples are written in PHP, but the procedure can be implemented in any language imaginable.
  • Let us take Steemit's white paper as an example.

Split the file

Splitting the file is easy on the right.

Procedure

  • We're looking at the size of the file,
  • split the file into many small sections with fread($file_handle, $buffer),
  • done

Partial section Example

// ...

// how many parts would be existed
$parts = $file_size / $buffer;

// ...

for ($i = 0; $i < $parts; $i++) {
    // read buffer sized amount of the file
    $file_part = fread($file_handle, $buffer);

    // the filename of the part
    $file_part_path = $store_path.$file_name.".part$i";

    // create and write the part
    $file_new = fopen($file_part_path, 'w+');
    fwrite($file_new, $file_part);
    fclose($file_new);
}

// ...

You can find the complete example in split.php

Upload the file

Here's the interesting part.
If we now have our small pieces, we have to convert every single piece into text that we can upload as mail.

Procedure

  • We'll take a section,
  • read the contents of the file with file_get_contents(),
  • convert the binary content to text. For example with bin2hex(),
  • write a new post with this content

Example

// ...

$fileData = bin2hex(file_get_contents('PATH_TO_FILE/steem-whitepaper.pdf.part0'));

// ...

You can find the complete example in upload.php

Download the file

For the whole thing to make sense, we have to get the file out of the blockchain.

With the LightRPC Client (https://github.com/hernandev/light-rpc) it is an easy to read single posts via PHP.
If you don't know how to install it, just have a look at https://github.com/hernandev/light-rpc There is a wonderful explanation.

If requested, I can also write a small tutorial for you here.

Procedure

  • We're reading our summary file,
  • get the contents of every post,
  • convert this content back to binary data,
  • pack all contents into one file.

In our example file we have moved the download part into a function, so that the whole thing is a bit clearer.


/**
 * This function fetch a steemit post
 *
 * @param string $username
 * @param string $permlink
 * @return mixed
 */
function getContent($username, $permlink)
{
    $Client = new Client('https://api.steemit.com');

    $Request  = new Request('content_api', 'get_content', [$username, $permlink]);
    $Response = $Client->send($Request);

    if ($Response->isError()) {
        var_dump($Response->error());
        exit;
    }

    $response = $Response->toArray();
    $result   = $response['result'];

    return $result['body'];
}

Now, to the "how we get it all together" part.

In our $summary we have all our files, we go through them one by one and put our whitepaper
back into one file.


// now we go through all the files and get the content from the steem blockchain
foreach ($summary as $part => $permlink) {
    echo $part.PHP_EOL;

    // get the content from the steem blockchain with our helper function
    $content = getContent('pscg.test', $permlink);

    // the content is in hex text, but we need binary.
    // if you still remember we converted it for the steem blockchain
    // now we convert it back to binary data
    $content = hex2bin($content);

    // this binary data is now attached to our file
    file_put_contents($filename, $content, \FILE_APPEND);
}

You can find the complete example in download.php

I hope the tutorial was fun and taught you a little bit.

I wish you a lot of fun
Hen

Sort:  

Hey @dehenne,
That is such a cool idea, and nice approach to implementing it!
As @portugalcoin mentioned, following the template is normally preferred, yet your post does bring additional value of course to our tutorials.
It would have been interesting to see this as a live example with actual screenshots and outcome. If you did not want to try this out on live, maybe you can check on a Steem testnet https://developers.steem.io/testnet/ ? I haven't tried it myself but could be worthy in exploratory situations.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thank you for the approving. In real live example is to be enjoyed with caution. After my vacation I can think about setting something up for the Testnet. ;-)

Hey @dehenne
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Thank you for your contribution.

  • Doesn't follow the template for the tutorials link : You don't have the repository and Proof of Work Done.

Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Really cool! I will give this a try

Congratulations @dehenne! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

Support SteemitBoard's project! Vote for its witness and get one more award!

Coin Marketplace

STEEM 0.27
TRX 0.12
JST 0.031
BTC 61785.67
ETH 2891.86
USDT 1.00
SBD 3.54