next.jsfrontendimage-manipulation

Is there any way to see the date on which an image was taken in Next.JS


I'm creating a photo gallery application using Next.JS, AWS S3, ImageKit and MongoDB. For a bit of context, I already had a photo-gallery app made, but it relied on an external express.js API that I was hosting on a VPS, which seemed a bit unnecessary and overcomplicated for my use-case.

So far, it is going really well, but I've hit a bit of a roadblock.

I am using presigned URLs to upload the images to S3, as uploading 4K images through serverless functions is an absolute no-go. But I cannot access the same properties that I could when I was uploading through express.

I can see the date on which a photo was last modified, but that is not very useful, especially when an image has been copied from a network drive or downloaded from Google Drive or OneDrive.

Is there any way that I could access the date on which a file (in this case an image) was created/taken without the use of an external API as I was doing beforehand?


Solution

  • You can always use the stat function that is found in the NodeJS Filsystem API (fs), note that there’s also a birthtimeMs property in the stats object holding the file’s created date in milliseconds:

    import fs from "fs/promises";
    
    export async function getCreatedAt(file: string): Promise<Date> {
      const { birthtime } = await fs.stat(file);
      return birthtime;
    }
    
    // or if you want the milliseconds
    
    export async function getCreatedAtMs(file: string): Promise<number> {
      const { birthtimeMs } = await fs.stat(file);
      return birthtimeMs;
    }