Skip to content

Support for concatenating multiple files #38

@ikreymer

Description

@ikreymer

It would be great if there was documentation / examples, or even a utility function for concatenating existing file links, to make a new file. After debugging, I was able to come up with something that works, but not sure if this is the 'right' way to do it, and doesn't using the task queue...

The writer is the top-level writer from UnixFS.createWriter() and links is an array of links returned after multiple fileWriter.close() calls.
The below produces a concatenated file, using createAdvancedFile.

async function concat(writer, links) {
  const {fileEncoder, hasher, linker} = writer.settings;
  const advanced = fileEncoder.createAdvancedFile(links);
  const bytes = fileEncoder.encode(advanced);
  const hash = await hasher.digest(bytes);
  const cid = linker.createLink(fileEncoder.code, hash);
  const block = { bytes, cid };
  
  // correct to access underlying writer like this?
  writer.writer.write(block);

  const link = {
    cid,
    contentByteLength: fileEncoder.cumulativeContentByteLength(links),
    dagByteLength: fileEncoder.cumulativeDagByteLength(bytes, links),
  };

  return link;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions