On Wed, 02 Oct 2013 01:36:52 GMT, Mike wrote:

Hi,

Is there a base64 encoding example? I'm trying to convert an image a person uploads into base64 so i can store it into redis.

I'm kind of new to D, so any help would be appreciated.

Thanks!

See the second example at the top of http://dlang.org/phobos/std_base64.html. You basically just need to put the input data as a ubyte[] array into Base64.encode and you'll get the base 64 string out. MIME encoding also requires line breaks at regular intervals, which is why the byChunk approach is used there. Translated to vibe.d primitives this would look like this (byChunk is only available for std.stdio.File):

import vibe.core.file : openFile;
import std.base64 : Base64;
import std.range : chunks;
import std.stdio : writeln;

// Create MIME Base64 with CRLF, per line 76.
auto f = openFile("./text.txt");
scope(exit) f.close();

Appender!string mime64 = appender!string;

for (ulong i = 0; i < f.size; i += 57) {
  ubyte[57] buf;
  auto chunk_size = min(f.size - i, buf.length);
  f.read(buf[0 .. chunk_size]);
  mime64.put(buf[0 .. chunk_size);
  mime64.put("\r\n");
}

writeln(mime64.data);