Logo

dev-resources.site

for different kinds of informations.

How to work with CAR files with NestJS

Published at
1/2/2025
Categories
carfile
nft
nestjs
ipfs
Author
burgossrodrigo
Categories
4 categories in total
carfile
open
nft
open
nestjs
open
ipfs
open
Author
14 person written this
burgossrodrigo
open
How to work with CAR files with NestJS

What is a CAR File?

A CAR file is a representation of a set of blocks in a DAG (Directed Acyclic Graph) structure. It's widely used in decentralized storage protocols like IPFS for its ability to store data efficiently while ensuring content addressability via CIDs (Content Identifiers).

Steps to Compact Multiple Files into a CAR File

  1. Prepare the Files
    Convert the files into byte arrays or readable streams.
    Ensure the files are properly labeled with metadata if needed.

  2. Use ipfs-car or nft.storage Tools
    Tools like ipfs-car are libraries to create, manipulate, and encode CAR files.

  3. Create a CAR Encoder
    Use the createFileEncoderStream, createDirectoryEncoderStream, or CAREncoderStream to encode the files into CAR format.

Implementation Example

Here’s how to implement it in a NestJS service using ipfs-car:

File controller:

import { Controller, Post, UploadedFiles, UseInterceptors } from '@nestjs/common';
import { FilesInterceptor } from '@nestjs/platform-express';
import { FileService } from './file.service';

@Controller('file')
export class FileController {
  constructor(private readonly fileService: FileService) {}

  @Post('upload-car')
  @UseInterceptors(FilesInterceptor('files'))
  async uploadFiles(@UploadedFiles() files: Express.Multer.File[]) {
    const rootCID = await this.fileService.packFilesIntoCar(files);
    console.log(`Root CID: ${rootCID}`);
    return { rootCID };
  }
}
Enter fullscreen mode Exit fullscreen mode

File Service:

import { Injectable } from '@nestjs/common';
import { Blob } from 'nft.storage';
import { CAREncoderStream } from 'ipfs-car';

@Injectable()
export class FileService {
  constructor() {}

  public async packFilesIntoCar(files: Express.Multer.File[]): Promise<string> {
    let rootCID = '';

    try {
      const blobs = files.map((file) => {
        const fileBytes = new Uint8Array(file.buffer);
        return new Blob([fileBytes], { type: file.mimetype });
      });

      // Start encoding files into CAR format
      const encoder = new CAREncoderStream();

      // Add all files to encoder
      for (const blob of blobs) {
        const fileStream = blob.stream();
        const writer = encoder.getWriter();
        for await (const chunk of fileStream) {
          writer.write(chunk);
        }
        writer.close();
      }

      const rootBlock = await encoder.getRootBlock();
      rootCID = rootBlock.cid.toString();

      return rootCID;
    } catch (error) {
      throw new Error(`Error creating CAR file: ${error.message}`);
    }
  }
}

Enter fullscreen mode Exit fullscreen mode

Explanation

File Controller:

Exposes an endpoint to upload files (/upload-car).

  • Intercepts files via FilesInterceptor (Multer middleware).
  • Calls the packFilesIntoCar method from the FileService. File Service:

Converts files to Blob objects.

  • Uses CAREncoderStream to encode files into a CAR file.
  • Returns the root CID of the CAR archive.

CAREncoderStream:

  • Writes file blocks to the CAR archive. Generates a root CID for referencing the CAR file.

Benefits

  • Data Integrity: Each block in the CAR file has its unique CID.
  • Compatibility: CAR files are natively supported in IPFS and Filecoin.
  • Efficiency: Combines multiple files into a single compact archive.

Additional Enhancements

  • Save the CAR file locally: Use Node.js fs module to write the CAR stream into a file.

  • Upload CAR to Decentralized Storage: Integrate with nft.storage, Web3.Storage, or similar APIs.

  • Add Metadata: Include additional metadata like file names, timestamps, or descriptions to make the archive more useful.

nestjs Article's
30 articles in total
Favicon
Understanding CORS and Setting it up with NestJS + Fastify 🚀
Favicon
Crudify: Automate Your Mongoose CRUD Operations in NestJS
Favicon
Building "Where Am I?": A GeoGuessr Alternative for Mobile
Favicon
Utilizando la librería Mongoose
Favicon
How to easily send emails in NestJS?
Favicon
Why NestJS Is The New Gold Standard For Node Backend Development
Favicon
Designing RBAC Permission System with Nest.js: A Step-by-Step Guide
Favicon
How to work with CAR files with NestJS
Favicon
Handle Payments Module in NestJS
Favicon
What we're working on: Database Entities
Favicon
Deploy NestJS and NextJS application in same server using pm2 and Nginx
Favicon
Advance nest.js
Favicon
Converting date by user time zone in "NestJS", and entering and displaying date in "Angular"
Favicon
Web Performance Optimization Tips from My Latest Project
Favicon
Blogsphere | A blogging website made with MERN stack. includes user management.
Favicon
nest.js
Favicon
[Boost]
Favicon
Wanna test NestJS out - any tips
Favicon
nestJS Modern Framework for Scalable Applications
Favicon
🚀 Applying SOLID Principles in NestJS: A Practical Guide
Favicon
Nestjs
Favicon
Which One Should You Choose NEST JS or EXPRESS JS?
Favicon
Integrating and storing the selected user language into the database in a full-stack application on "Angular" and "NestJS"
Favicon
Hello,help review my fullstack website stack : nestjs,mongodb and reactjs. https://events-org-siiv.vercel.app/
Favicon
The Ultimate Tech Stack for Startups in 2025
Favicon
NestJS and more
Favicon
Como implementar Feature Flags em seu Backend NestJS
Favicon
Building an Image Moderation System with AWS Rekognition, Nest.js, and React
Favicon
How to Create a quick Authentication library for NestJS/MongoDB application
Favicon
Understanding DTOs (Data Transfer Objects) in NestJS🚀

Featured ones: