Exemplo n.º 1
0
        public string Rename(int folderId, string newFolderName)
        {
            string success = "";

            try
            {
                string oldName;
                using (var db = new OggleBoobleMySqlContext())
                {
                    var dbSourceFolder = db.CategoryFolders.Where(f => f.Id == folderId).FirstOrDefault();
                    oldName = dbSourceFolder.FolderName;
                    string parentPath = Helpers.GetParentPath(folderId);
                    string ftpPath    = ftpHost + dbSourceFolder.RootFolder + hostingPath + parentPath + oldName;

                    string[] serverSideFiles = FtpUtilies.GetFiles(ftpPath);
                    string   fileNameGuid    = "";
                    string   newFileName     = "";
                    foreach (string serverSideFile in serverSideFiles)
                    {
                        fileNameGuid = serverSideFile.Substring(serverSideFile.LastIndexOf("_"));
                        newFileName  = newFolderName + fileNameGuid;
                        FtpUtilies.RenameFile(ftpPath + "/" + serverSideFile, newFileName);
                    }
                    success = FtpUtilies.RenameFolder(ftpPath, newFolderName);
                    if (success == "ok")
                    {
                        dbSourceFolder.FolderName = newFolderName;
                        db.SaveChanges();
                    }
                }
                //if (success == "ok")
                //{
                //    using (var mdb = new OggleBoobleMySqlContext())
                //    {
                //        var mySqlCategoryFolder = mdb.CategoryFolders.Where(f => f.Id == folderId).FirstOrDefault();
                //        mySqlCategoryFolders.FolderName = newFolderName;
                //        mdb.SaveChanges();
                //    }
                //}
            }
            catch (Exception ex)
            {
                success = Helpers.ErrorDetails(ex);
            }
            return(success);
        }
Exemplo n.º 2
0
        private string RenamePhyscialFiles(string ftpPath, string folderName, RepairReportModel repairReport)
        {
            string success;

            try
            {
                string   fileName, newFileName, ext, possibleGuid = "";
                string[] physcialFiles = FtpUtilies.GetFiles(ftpPath);
                if ((physcialFiles.Length == 1) && (physcialFiles[0].StartsWith("ERROR")))
                {
                    return(physcialFiles[0]);
                }
                for (int i = 0; i < physcialFiles.Length; i++)
                {
                    fileName = physcialFiles[i];
                    ext      = fileName.Substring(fileName.LastIndexOf("."));
                    if (ext == ".webp")
                    {
                        ext         = ".jpg";
                        newFileName = fileName.Substring(0, fileName.LastIndexOf(".")) + ".jpg";
                        FtpUtilies.RenameFile(ftpPath + "/" + fileName, newFileName);
                        repairReport.ExtensionChanged++;
                    }
                    if (fileName.LastIndexOf("_") - 1 < -1)
                    {
                        possibleGuid = Guid.NewGuid().ToString();
                        //possibleGuid = null;
                    }
                    else
                    {
                        if (fileName.Substring(fileName.LastIndexOf("_") + 1).Length < 36)
                        {
                            possibleGuid = Guid.NewGuid().ToString();
                        }
                        else
                        {
                            possibleGuid = fileName.Substring(fileName.LastIndexOf("_") + 1, 36);
                            Guid.TryParse(possibleGuid, out Guid outGuid);
                            if (outGuid == Guid.Empty)
                            {
                                possibleGuid = Guid.NewGuid().ToString();
                            }
                        }
                    }
                    try
                    {
                        if (fileName != (folderName + "_" + possibleGuid + ext))
                        {
                            newFileName = folderName + "_" + possibleGuid + ext;
                            FtpUtilies.RenameFile(ftpPath + "/" + fileName, newFileName);
                            repairReport.PhyscialFileRenamed++;
                        }
                        else
                        {
                            newFileName = "ok";
                        }
                    }
                    catch (Exception ex)
                    {
                        newFileName = Helpers.ErrorDetails(ex);
                        //newFileName = folderName + "_" + Guid.NewGuid().ToString() + ext;
                        //FtpUtilies.RenameFile(ftpPath + "/" + fileName, newFileName);
                        //repairReport.PhyscialFileRenamed++;
                    }
                    //}
                    //else
                    //{
                    //    newFileName = folderName + "_" + Guid.NewGuid().ToString() + ext;
                    //    FtpUtilies.RenameFile(ftpPath + "/" + fileName, newFileName);
                    //    repairReport.PhyscialFileRenamed++;
                    //}
                    //}
                    //else
                    //{
                    //    newFileName = folderName + "_" + Guid.NewGuid().ToString() + ext;
                    //    var sss = FtpUtilies.RenameFile(ftpPath + "/" + fileName, newFileName);
                    //    if (sss == "ok")
                    //        repairReport.PhyscialFileRenamed++;
                    //}
                }
                success = "ok";
            }
            catch (Exception ex)
            {
                success = Helpers.ErrorDetails(ex);
            }
            return(success);
        }