Deletion of folders in the SharedFiles module

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
7/29/2008 10:25:05 AM
JMJ
Gravatar
Total Posts 5

Deletion of folders in the SharedFiles module

Joe,

In the SharedFiles module, when you delete a folder, all the files inside it and the sub-folders remain in the DB and on the disk. The method that should do the job is there but not implemented yet. Here is the way I implemented it :

File: SharedFileFolder.cs

public void DeleteAllFiles(String sourcePath)
{
// TODO: implement

IDataReader reader;
reader = SharedFile.GetSharedFiles(ModuleId, FolderId);
while (reader.Read())
{
SharedFile sharedFile = new SharedFile(ModuleId, Convert.ToInt32(reader["ItemID"]));
sharedFile.Delete(sourcePath);
}
reader.Close();

reader = SharedFileFolder.GetSharedFolders(ModuleId, FolderId);
while (reader.Read())
{
SharedFileFolder folder = new SharedFileFolder(ModuleId, Convert.ToInt32(reader["FolderID"]));
folder.DeleteAllFiles(sourcePath);
SharedFileFolder.DeleteSharedFileFolder(Convert.ToInt32(reader["FolderID"]));
}
reader.Close();

}

As you will notice, I had to change the signature of the method so you will also have to adjust the call to it in the method DeleteItem of the file SharedFiles.ascx.cs .

Thanks

Jean-Michel

7/29/2008 10:37:05 AM
Gravatar
Total Posts 18439

Re: Deletion of folders in the SharedFiles module

Hi Jean-Michel,

Thanks for posting this, but before I can use it it needs to be changed a little. We need to get a DataTable instead of a reader so that we are not holding an open database connection while we loop through and delete, because each delete will open and close a connection. Your solution will work for most dbs but it definitely won't work with SQLite because SQLite has really only 1 connection available. Even for multithreaded dbs with connection pools its still would be better to use a DataTable in this case. I like DataReader for data binding but not for operations where I'm processing rows with other db calls. If you can implement methods that populates a DataTable with the needed ids and then call that method to get the items to delete instead of IDataReader, I will accept your code.

My only other concern is, I think we should be checking the module setting and find out if file versioning is enabled, if we are keeping revision history of files it may be best not to actually delete them so that we can recover deleted versions of the files. I'm pretty sure this concern is why I left it unimplemented at the time and planned to get back to it after figuring out how that should be handled.

Best,

Joe

7/31/2008 12:08:30 PM
JMJ
Gravatar
Total Posts 5

Re: Deletion of folders in the SharedFiles module

Joe,

I'm using MSSQL,MySQL exclusively so I forget easily about other DBs limitations; thanks for bringing that point. Since I only need the IDs of the files and folders, I did put them in ArrayLists while I am looping for the deletes. I think that this is more lightweight than using datatables and it leave the code localised in the same method.
Here is my new version :

public void DeleteAllFiles(String sourcePath)
{
// TODO: implement

ArrayList folders = new ArrayList();
ArrayList files = new ArrayList();
IDataReader reader;

reader = SharedFile.GetSharedFiles(ModuleId, FolderId);
while (reader.Read())
{
files.Add(Convert.ToInt32(reader["ItemID"]));
}
reader.Close();

reader = SharedFileFolder.GetSharedFolders(ModuleId, FolderId);
while (reader.Read())
{
folders.Add(Convert.ToInt32(reader["FolderID"]));
}
reader.Close();

foreach (int id in files)
{
SharedFile sharedFile = new SharedFile(ModuleId, id);
sharedFile.Delete(sourcePath);
}

foreach (int id in folders)
{
SharedFileFolder folder = new SharedFileFolder(ModuleId, id);
folder.DeleteAllFiles(sourcePath);
SharedFileFolder.DeleteSharedFileFolder(id);
}
}

Regarding your concern about the versioning; I did not find a module setting that let you enable or disable it. It is always enabled and it brings a couple of other issues with the deletion of files. Here are my findings:
1- You cannot delete a file that has historic versions on it; when it tries to delete the row in the mp_SharedFiles table, it complains about a foreing key violation in the mp_SharedFilesHistory table. Unfortunately at that point the physical file is already destroyed; so you cannot even try to restore a previous version of the file. The only way to get out of that state is to cleanup manually the DB.
2- There is no way of removing individual historic versions of a file. Maybe this is by design but it is not very forgiving if you upload something by mistake. Plus because of problem #1 once you've uploaded a new version your file cannot be deleted.
3- If you are on the main page of the module (Sharedfiles.ascx) and you delete a file that has no history on it, it works fine; but if you go into the edit page and you try to delete the file with the delete button, the handler behaves differently, it creates an historic version of the file before the delete which puts you in the state of the problems mentioned above. Plus even if it would works , how could you restore the historic versions since the file does not exist anymore ?
My opinion is that when you delete a file , you should flush all the historical versions that comes with it.

By the way this is not a request for you to put time on this, I do not need this functionality ; I've just discovered these problems as I was modifying the module to include some user security on folders. If you want my help just let me know what route you want me to take for the fixes and I'll try to take a look at it when I have 2 minutes.

BTW I am really impressed with the quality of your code, plus attacking so many DBs with the same code base is quite an accomplishment. I am glad that I found Mojo :-).

Thanks
Jean-Michel
 

7/31/2008 12:27:13 PM
Gravatar
Total Posts 18439

Re: Deletion of folders in the SharedFiles module

Hi Jean-Michel,

Your ArrayList solution works just as good as a DataTable. I'll go ahead and add this change.

If you'd like to put a little love into improving the Shared Files feature and tackling some of the issues you have found that would be great. I'm working on some other things right now and really can't let myself get sidetracked on this, so your help would be appreciated. If/when you do complete the improvements, just zip up the modified files and send me the zip and I'll review it an integrate your changes. If you can put some comment markers in the code to help me find things you've changed that would be great. Also if you send it to me include in the email a statement that the changes are your original work and that you agree your contribution is licensed under the CPL.

I will go ahead and add the missing module setting, it will be in svn trunk by tonight but won't actually do anything until the code is modified to check it.

Best,

Joe

joe dot audette at g mail dot com

You must sign in to post in the forums. This thread is closed to new posts.