locked
deleting source of images RRS feed

  • Question

  • User237014454 posted

    i have a chatting website, and i created aspx page with upload control for users to upload their personal images, after limiting size, validate type jpg,bmp and recieving file content and using bitmap and graphics classes i create new resized one with watermark, the problem when saving file as jpeg type i want to overwrite old image if found, after that updating phys path into database for this user, then i want to ask a questions:-

    is it safe to overwrite image or it's better to save it with auto generated filename and update database with it,and store old name so i can after period of time delete it.  

    Friday, May 12, 2006 3:45 PM

All replies

  • User1109032460 posted
    Definitely go with the second option.
    Sunday, May 14, 2006 1:56 PM
  • User237014454 posted
    thx, but what is the best solution to set a file to be deleted after certain period of time
    Sunday, May 14, 2006 4:30 PM
  • User-1948650966 posted

    Save the first uploaded image as "myavatar.jpgg", load it, resize it, save it as "myavatar.jpg", dispose loaded image, delete "myavatar.jpgg". That's the way I'm using. There should be no problem and no need to extra delete process.

    Save each image with users nick name or just the ID from database. After the resize process you can delete ancient file and save the new one resized from "myavatar.jpgg".

    Monday, May 15, 2006 11:34 AM
  • User237014454 posted
    the problem if the user have already old image which is used by server then deleting it or overwriting may cause errors
    Monday, May 15, 2006 9:08 PM
  • User-1948650966 posted
    Why that should cause error? I'm using this system in a forum and there is no problem.

    If you mean those problems;

    -While trying to delete the file, that file could be in use because of a user request.
    -After the file deleted, till the new file replacement the user can't view his avatar.

    So the second isn't a real problem because that will occur for an half second. You can handle it by code and while the avatar replaces that user will not have an avatar for miliseconds maybe.

    For the first one, I never get that error in logs in my forum that got around 1000 users. But maybe that can happen in more populated forums. The solution could be to save the new file with a new name, insert that name in DB so new user requests pull the new image file, then you delete the ancient file. All those can be done in a row after the users new avatar upload. And for file names maybe u can use a System.Guid.NewGuid.ToString.
    Tuesday, May 16, 2006 3:51 AM
  • User237014454 posted
    thx a lot, and i think that DB solution will safe me from the problem (1st one u mentioned)
    Tuesday, May 16, 2006 9:35 AM
  • User-1624456073 posted
    The solution could be to save the new file with a new name, insert that name in DB so new user requests pull the new image file, then you delete the ancient file. All those can be done in a row after the users new avatar upload. And for file names maybe u can use a System.Guid.NewGuid.ToString.


    But then, if you have any caching on pages, those will still be linking the old image... and of course there's the remote possibility of someone having linked your image from another site, or it could be indexed by some image search engine for that matter :)

    I'd go for a solution which tries to delete the file within a try/catch, and in the case where an exception is thrown because the file has a read lock on it I would mark it for deletion in some maintenance log. Then, just take down your site for a few minutes every once in a while at a convenient time of day for your users (ie middle of the night), and run some cleanup tool that checks the maintenance log and deletes the troublesome files then :)

    Cheers,
    Fredrik
    Tuesday, May 16, 2006 7:36 PM
  • User-1948650966 posted
    Caching on a chat application? :) Could be a bad idea :)

    But try an other solution that doesnt need scheduling for file delete...

    Stream your avatar images through aspx files like;

    <img src="avatar.aspx?ID=22"> :)

    So this url will never change and cause any caching problem, but each time that URL calls will get the file name from DB and read the file that exist. And be sure that avatar.aspx could never be cached :) The ID cane be the users DB PK :) of course some encsryiption for that could be better because givind the PK freely to users could cause security threads. I always suggest to encrypte querystrings.

    Have fun :)
    Wednesday, May 17, 2006 3:14 AM
  • User-1624456073 posted
    Good point about not caching, didn't register with me that it was a chat application lol :)

    Anyways, that sounds like a good solution. Then the serving of the image and the actual reading of the file will be separated, leaving you to delete the old file without worries. However go for a http handler (.ashx) instead of an aspx, as it'll be faster and more lightweight (an aspx carries the overhead of the entire page lifecycle, which just pushing an image onto the response stream won't be needing). The handler could (and should) cache the image's it's serving; you would just have to make sure to invalidate the cache of an image whenever it changes :)
    Wednesday, May 17, 2006 6:17 AM
  • User237014454 posted
    thx for your help, this is really what i am using in my app like: getimg.ashx?uin=no to get the img, and for retrieving img open a new file stream with read access then read file content in byte array and return by response.binarywrite() with content type image/jpeg, and i think with the DB mechanism as i mentioned first will be safe enough and i can try to delete the file imm. after upload new file & changing new name in DB in try-catch also, if failed(i think this will be hard to happen) add this file name to certain log or leave it :), again thx for all of u
    Thursday, May 18, 2006 2:17 PM