2

I am developing a presentation style app (e.g. Powerpoint, Keynote) in Electron where the user can import their image files into a project folder and I am wondering how to handle the issue of potential file name conflicts.

I don't want to be reinventing wheels: are there schema, design patterns or frameworks for this sort of thing? For example on OSX, iPhoto uses date, time of import plus seemingly random(?) ids to organize imported images.

I will be implementing this is Javascript but am interested primarily in how to approach the problem so language doesn't matter.


iPhoto Library:

enter image description here

spring
  • 18,009
  • 15
  • 80
  • 160

1 Answers1

1

There are two approaches I am aware of:

Unique folder names

By placing each file in a folder with a unique name, you can preserve the original file name and avoid any clashes. This method will make things much easier for you if you want to store associated files (e.g. image thumbnails) with the original.

You can generate unique folder names using a UUID, which is a string that is virtually guaranteed not to clash. There are plenty of Node.js libraries to generate a UUID, such as uuid. Or just use any random string and check if the folder already exists to be sure.

Alternatively, as in the example you gave, the folder names could be generated according to date and time, but you have to be sure that you will only add one item at one point in time, or add random folders under the date as in the iPhoto example. The dated folders in iPhoto are probably not necessary when they are also using random-strings as folder names, but they would make it friendly for a user who is manually browsing through the folders, and there may be performance benefits if iPhoto needs a directory listing for a specific date.

You need to store a reference to both the folder name and the file name in order to load the file, but of course this could be one string e.g. "6c84fb90-12c4-11e1-840d-7b25c5ee775a/image.jpg".

Unique file names

Another technique is to rename files to have a unique name whenever there is a clash. This is the approach used by the macOS Finder when you create new folders or duplicate a file. This approach is usually best if the user may interact directly with the files, as they will not have to navigate through folders with meaningless names.

As a simple example, let's say I am adding photos of penguins, and I've already added a photo called penguin.jpg.

Now I add a second photo which also happens to be called penguin.jpg.

  1. Check if penguin.jpg exists. It does, so...
  2. Generate a new name for the file, penguin-2.jpg
  3. Check if penguin-2.jpg exists. It doesn't, so...
  4. Save the new file as penguin-2.jpg

If I add more files also called penguin.jpg, the program needs to keep incrementing the name until I find one which does not exist (e.g. penguin-3.jpg). This should not cause any performance issues unless adding thousands of files with the same name (which seems unlikely).

I found a Node.js module which can handle this approach for you: uniquefilename

Justin Emery
  • 1,644
  • 18
  • 25
  • Hey thanks, that's helpful. One concern I have about the whole "uuid" is crashing into the [260 char limit for paths under Windows](http://stackoverflow.com/questions/1880321/why-does-the-260-character-path-length-limit-exist-in-windows) since my app is x-platform. A few nested directories, a uuid and an absolute path could be >= 260. – spring Mar 15 '17 at 13:25
  • You could use a much shorter random string, e.g. with `Math.random().toString(36).substring(7)`, just check for any existing folder with the same name in case of a collision (which is still very unlikely, but could happen) – Justin Emery Mar 15 '17 at 13:29