Welcome Guest, Not a member yet? Register   Sign In
[solved] Finding a bug when you don't find the right action to make a situation when bug appears
#1

[eluser]SPeed_FANat1c[/eluser]
Hi,

I will try describe the algorithm.

I have a website, with articles. Administrator can upload image files and then put them into the article.
Sometimes it can happen that admin uploads file but does not save the article and closes the browser. So the file isn't needed anymore - it is a trash in the server.
To solve this - I made a table, which contains filenames together with article id's. When admin saves the article - the filenamaes of images to that article are also added to database table.

So now we can make a function which deletes files that are not in the database - by checking the table for a file name, and if there is not such filename in table, it removes it from server.

Now the problem - somehow after some time I found that many of the file names are gone from the database table, which are actually needed. So the function which cleans unneeded files cleans also the needed ones, because it does not find them in a table.

But I cannot find when do they dissapear from the table or maybe they are not inserted at all. I try myself to write artile, upload files, remove files, edit article, edit and remove files and everything works well while I am trying. I also tried searching for the table name in all CI instalation files, and only there are 3 files where that table name is used, so I checked the code and tried again to make that code execute to make all situations and still cannot find where it could not work.

So don't know what to do, maybe I miss something. What to do in such situation? Smile leave everything as it is and wait until something goes wrong? Smile

Edit:

I cannot leave the clean trash function, because I am not sure if everything will work. And yet I will have to manually insert the needed filenames into the filenames table with the article ids. Because there is no way to know for an algorithm which filename is for what article.
#2

[eluser]SPeed_FANat1c[/eluser]
Code:
function delete_picture_edit_ajax()
{
        
   $this->db->where('filename',$this->input->post('filename'))->delete('naujienu_nuotraukos');        
   if($this->membership->is_logged_in())
   {
        unlink('./uploads/naujienos/'.$this->input->post('filename'));
   }
   else echo 'not_admin';
}

in this function it might be that the record from database is removed when the admin is not logged in and the filename from server is not removed. But its hard to believe that because of this problem half of the filenames are removed. Admin should not even want to remove that much pictures.. But corrected this code - made it check if the admin is logged in before delete from db.

EDIT:

from another forum I got such answer:
Quote:First thing that you might want to do is to log all the pertinent info when you delete anything. The second thing that you might want to do (at least for a while) is to do a soft-delete instead. Just mark the record as deleted or save a copy under a special "deleted" key or move it to another "deleted record" table.

With both of these in place you will be able to investigate what is going on and you won't permanently lose any data.

So I will try to log and to soft delete and maybe will catch somehow when those unnecesary deletes happen.
#3

[eluser]Stoney[/eluser]
You can avoid this junk file problem like this:
1. first the article must be created before the file upload, so the files would be associated with the article.

or
2. create a new blank article, this way if the admin leaves the site, he leaves the files uploaded with empty article, which he will later finish.

or
3. your way, messy...

I prefer the 1st way.
#4

[eluser]toopay[/eluser]
use drafting... So admin can have a bunch of draft articles, admin can write then save then continue to write again then publish it.

With that scenario, now you can apply some rules, like give a warning that draft article(include all file which associated with it) will be deleted in 1x24 hours, if not published. Then, make some a cron to take care the rest of them Smile
#5

[eluser]SPeed_FANat1c[/eluser]
@cimet yeah, I like your idea, but I hate redoing things Smile my way is messy, I need to keep the filenames in the hidden input field and on save click I have to put them in db. But still my way should work and it works when I test. Just for some reason later on those filenames dissappear. But maybe I will redo it quicker in your way than find the real reason why filenames dissappiear from table..

I guess I will do it in the blank article way - because it might be annoying for the user when he will have to save an empty article just to upload images.

edit:

[quote author="toopay" date="1302187855"]use drafting... So admin can have a bunch of draft articles, admin can write then save then continue to write again then publish it.

With that scenario, now you can apply some rules, like give a warning that draft article(include all file which associated with it) will be deleted in 1x24 hours, if not published. Then, make some a cron to take care the rest of them Smile[/quote]

I think I don't need a bunch of them, just one - when admin clicks "create article" I will save it in db instatnly and later when he will be able to save changes whenever he clicks save button. If he will close the browser, the artivle still will be unpublished until he deletes it or publish. I think this way will be OK.


Edit2: so I made it using this

Quote:create a new blank article, this way if the admin leaves the site, he leaves the files uploaded with empty article, which he will later finish.


method. And I delete teh file rows from database using soft delete. We will see how will it go now. Thanks for the ideas Smile




Theme © iAndrew 2016 - Forum software by © MyBB