/// <summary> /// Delete specified file from database and file system. /// </summary> public string DeleteFile(Page page, string fileIDs) { if (fileIDs.Length == 0) { return("File is not specified"); } string [] s = fileIDs.Split(','); string sRez = ""; foreach (string sfile in s) { Common.Web.Page oPage = page as Common.Web.Page; Common.Web.Core oDB = oPage.DBase; DataRow oDR = oDB.GetFirstRow(Config.DbGetPushingContentFileDetails, "@ID", Utils.GetInt(sfile)); if ((oDR == null) || (oDR["Name"] == System.DBNull.Value) || (oDR["Name"].ToString() == String.Empty)) { return("File name not found."); } string fileName = oDR["Name"].ToString(); string serverRoot = Config.GetPushingContentPath(ContentID, false); if (!serverRoot.Trim().EndsWith("\\")) { serverRoot += "\\"; } string filePath = serverRoot + fileName; try { //first - delete from DB then delete from file system int iRet = oDB.Execute(Config.DbDeletePushingContentFile, "@IDs", Utils.GetInt(sfile)); if (iRet == 0) { sRez += "File " + fileName + " could not be deleted; "; } else { File.Delete(filePath); } } catch (Exception oEx) { Log.Write(this, "Fail to delete File. Error description: " + oEx.Message); sRez += "Error occured during deleting file " + fileName; } } return(sRez); }
/// <summary> /// Delete specified file from database and file system. /// </summary> public string DeleteFile(Page page, int fileID) { if (fileID <= 0) { return("File is not specified"); } Common.Web.Page oPage = page as Common.Web.Page; Common.Web.Core oDB = oPage.DBase; DataRow oDR = oDB.GetFirstRow(Config.DbGetFileDetails, "@ID", fileID); if ((oDR == null) || (oDR["FileName"] == System.DBNull.Value)) { return("File name not found."); } string fileName = oDR["FileName"].ToString(); string serverRoot = Config.GetAffiliatePath(AffID, false, false); if (!serverRoot.Trim().EndsWith("\\")) { serverRoot += "\\"; } string filePath = serverRoot + fileName; try { //first - delete from DB then delete from file system int iRet = oDB.Execute(Config.DbDeleteFileRelated, "@IDs", fileID); if (iRet == 0) { return("File " + fileName + " could not be deleted"); } else { File.Delete(filePath); } } catch (Exception oEx) { Log.Write(this, "Fail to delete File. Error description: " + oEx.Message); return("Error occured during deleting file " + fileName); } return(""); }
/// <summary> /// Process saved file /// </summary> public string ProcessFile(Page page, int ContentTypeID, string filePath, string fileName, int fileSize, ref int fileID) { int originalFileID = fileID; string fileExtension = Path.GetExtension(filePath).Replace(".", ""); string uploadUrl = Config.GetAffiliatePath(AffID, false, true); if (!uploadUrl.Trim().EndsWith("/")) { uploadUrl += "/"; } string serverRoot = Config.GetAffiliatePath(AffID, false, false); if (!serverRoot.Trim().EndsWith("\\")) { serverRoot += "\\"; } //1. Update DB Common.Web.Page oPage = page as Common.Web.Page; Common.Web.Core oDB = oPage.DBase; SqlCommand oCmd = oDB.GetCommand(Config.DbSaveFile); SqlParameterCollection oParams = oCmd.Parameters; oParams.Add("@fileID", fileID); oParams.Add("@fileName", fileName); oParams.Add("@fileSize", fileSize); oParams.Add("@fileExtension", fileExtension); oParams.Add("@urlRoot", uploadUrl); oParams.Add("@ContentTypeID", ContentTypeID); oParams.Add("@AffID", AffID); SqlParameter oParam = new SqlParameter("@fileNameOld", System.Data.SqlDbType.VarChar, 250); oParam.Direction = ParameterDirection.Output; oParams.Add(oParam); fileID = oDB.ExecuteReturnInt(oCmd); string fileNameOld = oCmd.Parameters["@fileNameOld"].Value.ToString(); if (fileID <= 0) { try { File.Delete(filePath); }catch (Exception oEx) { Log.Write(this, "Fail to delete file. Error description: " + oEx.Message); } if (fileID < 0) { return("File with name " + fileName + " already exists. Please change name of file and try again. "); } else { return("Database error occured during saving file " + fileName + ". "); } } //Everything is ok. We can rename just uploaded file with permanent name //delete old file if (originalFileID > 0) { try { string filePathOld = serverRoot + fileNameOld; File.Delete(filePathOld); } catch (Exception oEx) { oDB.Execute(Config.DbDeleteFile, "@IDs", fileID); Log.Write(this, "Fail to delete old file. Error description: " + oEx.Message); return("Error occured during delete old file " + fileNameOld); } } //rename uploaded file to permanent name try { string filePathNew = serverRoot + fileName; if (File.Exists(filePathNew)) { File.Delete(filePathNew); } File.Move(filePath, filePathNew); } catch (Exception oEx) { oDB.Execute(Config.DbDeleteFile, "@IDs", fileID); Log.Write(this, "Fail to move file. Error description: " + oEx.Message); return("Error occured during moving file " + fileName + ". "); } return(""); }