Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
copy a file/object and change the name

I'm doing an API for a gallery; so, I create a method that let copy an image from the database. Now, I want to add a number at the end of the copy-image name. For example:

-original image name: image
-copy image name: image(1)
-2nd copy image name: image(2)

How can I add the number to the name of copied name automatically?

     'use strict'
        let imageObject= require('../models/image-object');
        let fs=require('fs');
        let path= require('path');
        
        
        
        let gallery_controllers={
        
     
    
           copyImage:function(req,res){         
                
                    //get the id param of the image to copy
                let imageId=req.params.id;
                if(imageId==null) return res.status(404).send({message:"no ID defined"});
        
                    //I search the requiere image on the database
                imageObject.findById(imageId,(err,image)=>{
                    if(err) return res.status(500).send({message:'err to response data'});
                    if(!image) return res.status(404).send({message:'image not found'});
        
                    if(image){
                    //set a new model-object
                        let imageCopied= new imageObject();
                        
                        imageCopied.name= image.name;
                        imageCopied.image=image.image;
        
                    //save image copied on the database
                        imageCopied.save((err,image_copied)=>{
                            if(err) return res.status(500).send({message:"error 500"});
                            if(!image_copied) return res.status(404).send({message:"error 404"});
        
                            return res.status(200).send({
                                image:image_copied
                            })
                        })
                    }
                })
        
            },
        }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Here's a function that looks in the directory passed to it for files of the name file(nnn) where nnn is some sequence of digits and returns back to you the full path of the next one in sequence (the one after the highest number that already exists).

This function pre-creates a placeholder file by that name to avoid concurrency issues with multiple asynchronous operations calling this function and potentially conflicting (if it only returned the filename without creating the file). To further handle conflicts, it creates the placeholder file in a mode that fails if it already exists (so only one invocation of this function will ever create that particular file) and it automatically retries to find a new number if it gets a conflict (e.g. someone else created the next available file before we got to it). All of this logic is to avoid the subtleties of possible race conditions in creating the next filename in the sequence.

Once the caller has a unique filename that this resolves to, then it is expected that they will overwrite the placeholder contents with their own contents.

// pass directory to look in
// pass base file name so it will look for next in sequence as in "file(3)"
// returns the full path of the unique placeholder file it has created
// the caller is then responsible for that file
// calling multiple times will create a new placeholder file each time
async function findNextName(dir, base) {
    let cntr = 0;
    const cntr_max = 5;
    const regex = new RegExp(`^${base}\\((\\d+)\\)$`);

    async function run() {
        const files = await fs.promises.readdir(dir);
        let highest = 0;
        for (let f of files) {
            let matches = f.match(regex);
            if (matches) {
                let num = parseInt(matches[1]);
                if (num > highest) {
                    highest = num;
                }
            }
        }
        let name = `${base}(${highest + 1})`;
        // create placeholder file with this name to avoid concurrency issues
        // of another request also trying to use the same next file
        try {
            // write to file, fail if the file exists due to a concurrency issue
            const fullPath = path.resolve(path.join(dir, name));
            await fs.promises.writeFile(fullPath, "placeholder", { flag: "wx" });
            return fullPath;
        } catch (e) {
            // if this fails because of a potential concurrency issue, then try again
            // up to cntr_max times to avoid looping forever on a persistent error
            if (++cntr < cntr_max) {
                return run();
            } else {
                throw e;
            }
        }
    }
    return run();
}

You could call it like this:

findNextName(".", "file").then(filename=> {
    console.log(filename);
}).catch(err => {
    console.log(err);
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!