Search code examples
angularjsazureimage-uploadingfileapi

Dealing with server stranded file uploads


I have an Angular SPA app running on Azure and I'd like to implement a rich text editor similar to Medium.com. I'll be using some existing editor for this, but I have a problem with image files.

The problem

I would like my editor to have the ability to also add images inside content. The problem I'm having is when should I be uploading images to the server?

Possible solution 1: Immediate upload after they're selected

The good

  • saving content is quicker because all images are likely already uploaded
  • files get displayed right after they're uploaded from the server URL

The bad

  • files may have to be deleted on the server if users selects to cancel editing
  • files may get stranded on the server if user simply closes their browser window

Possible solution 2: Upload after save

The good

  • files get displayed immediately using FileAPI capabilities
  • no stranded server side files if editing is discarded in whatever way

The bad

  • saving of content may take longer as all images need to be uploaded at the moment of saving content
  • additional client-side to display images from local files

Question

I would like to implement Solution 1 because it provides more transparent user interface process and reacts quicker to edit saves => better UX. But how should I be managing stranded files? I could use a worker process that would delete stranded files from time to time, but I wonder whether that's the best approach for this scenario.

What and how would you suggest I implement this?


Solution

  • This is highly subjective (opinion based), but I will give it a shot.

    You are actually having bigger problem than you think. In your abstracted approaches, you only describe a situation when user started something as new. Whereas I see much harder to solve issues if user is editing existing item. What will happen if he/she deletes images, adds new images and at the end hits CANCEL. And also, if Internet connection drops while creating / editing?

    I would also go for Solution one. And, of course minimize the "bad" things, as they aren't really that much or hard to handle. Here is how I would solve all the "bad"s in Approach 1:

    • All my articles (or whatever user is editing with editor) will have a boolean flag "IsDraft" or something like this. Then all my business logic for front end will only look for items where IsDraft == False.
    • Whenever a user starts a new article (the easiest to solve problem) I immediately create new item in my DB with IsDraft=True
    • Have a link table to keep link between ID of item being created and Image Files being used (blobs). The point here is, that, if you do not keep links between used and unused blobs, you will have a lot headaches deciding which blob to delete and which one to leave on the storage.
    • Have a worker process (either as worker process in Web Role if I use Cloud Services, or as a Web Job (+ nice and short explanation here) is I use Web Sites) that checks for articles that are Draft and older than XXX days. If found - delete files + article itself.

    Handling Editing of existing item is more challenging - for this, I might take the approach of:

    • Create a new copy of the entire article when user hits Edit and mark it as draft
    • If user hits save - switch the content of the new article (new version) with the existing one, leaving the new article marked as IsDraft - the worker process will clean it up.
    • If user doesn't hit save for some reason (hit cancel, or internet drops, or computer restarts, or browser crashes, or or or ..) - the new article will be cleaned later by the worker process

    And if you want to go deeper and crazier, you can have a section in your admin panel where you show the Drafts to your users, so they can either continue work, or leave it to be auto cleaned.