Exemplo n.º 1
0
        public string MoveImageToRejects(string linkId)
        {
            string success;

            try
            {
                using (var db = new OggleBoobleMySqlContext())
                {
                    var    dbSourceImage  = db.ImageFiles.Where(f => f.Id == linkId).First();
                    string dbRejectFolder = ftpHost + "/archive.OggleBooble.com/rejects/" + dbSourceImage.FileName;
                    var    dbSourceFolder = db.CategoryFolders.Where(f => f.Id == dbSourceImage.FolderId).First();
                    string ftpRepo        = imgRepo.Substring(7);
                    string sourceFtpPath  = ftpHost + ftpRepo + "/" + dbSourceFolder.FolderPath + "/" + dbSourceImage.FileName;
                    success = FtpUtilies.MoveFile(sourceFtpPath, dbRejectFolder);

                    var linksToRemove = db.CategoryImageLinks.Where(l => l.ImageLinkId == linkId).ToList();
                    db.CategoryImageLinks.RemoveRange(linksToRemove);



                    ImageFile reject = db.ImageFiles.Where(i => i.Id == linkId).First();
                    reject.FolderId = -6;
                    //db.ImageFiles.Remove(reject);
                    db.SaveChanges();
                    success = "ok";
                }
            }
            catch (Exception ex) { success = Helpers.ErrorDetails(ex); }
            return(success);
        }
Exemplo n.º 2
0
        private void PerformFolderChecks(int folderId, RepairReportModel repairReport, bool recurr)
        {
            List <CategoryFolder> childFolders = null;

            try
            {
                string ftpPath;
                string imageFolderName;
                string imageFolderPath;
                List <CategoryImageLink> catLinks;
                List <ImageFile>         folderImages;
                using (var db = new OggleBoobleMySqlContext())
                {
                    CategoryFolder dbCategoryFolder = db.CategoryFolders.Where(f => f.Id == folderId).First();
                    ftpPath         = ftpHost + "/" + imgRepo.Substring(8) + "/" + dbCategoryFolder.FolderPath;
                    imageFolderName = ftpPath.Substring(ftpPath.LastIndexOf("/") + 1);
                    childFolders    = db.CategoryFolders.Where(c => c.Parent == folderId).ToList();
                    folderImages    = db.ImageFiles.Where(if1 => if1.FolderId == folderId).ToList();
                    catLinks        = db.CategoryImageLinks.Where(l => l.ImageCategoryId == folderId).ToList();
                    imageFolderPath = dbCategoryFolder.FolderPath;
                    if (dbCategoryFolder.FolderType == "singleChild")
                    {
                        CategoryFolder dbParentFolder = db.CategoryFolders.Where(f => f.Id == dbCategoryFolder.Parent).First();
                        imageFolderName = dbParentFolder.FolderName;
                    }
                }
                //string folderName = dbCategoryFolder.FolderName;
                List <CategoryImageLink> dbRejCatlinks;
                string ftpFileName, physcialFileName, physcialFileLinkId, rejectFolder, ftpSuccess;
                string renameSuccess = RenamePhyscialFiles(ftpPath, imageFolderName, repairReport);
                if (renameSuccess != "ok")
                {
                    repairReport.Success = renameSuccess + "ftpPath: " + ftpPath + "  imageFolderName: " + imageFolderName;
                    return;
                }

                string[] physcialFiles = FtpUtilies.GetFiles(ftpPath);
                if (physcialFiles.Length > 0 && physcialFiles[0].StartsWith("ERROR"))
                {
                    repairReport.Success = physcialFiles[0];
                    return;
                }

                #region 0 rename ImageFiles to expected name
                List <ImageFile> dbFolderImageFiles;
                using (var db = new OggleBoobleMySqlContext())
                {
                    dbFolderImageFiles = db.ImageFiles.Where(if1 => if1.FolderId == folderId).ToList();

                    string expectedFileName;
                    foreach (ImageFile imageFile in dbFolderImageFiles)
                    {
                        if (imageFile.FileName.LastIndexOf(".") < 5)
                        {
                            imageFile.FileName = "xxxxx.jpg";
                            //repairReport.Errors.Add("bad filename: " + imageFile.FileName + "folder: " + imageFile.FolderId);
                        }
                        expectedFileName = imageFolderName + "_" + imageFile.Id + imageFile.FileName.Substring(imageFile.FileName.LastIndexOf("."));
                        if (imageFile.FileName != expectedFileName)
                        {
                            imageFile.FileName = expectedFileName;
                            db.SaveChanges();
                            repairReport.ImageFilesRenamed++;
                        }
                    }
                }
                #endregion

                #region 1. make sure every physicalFile has an ImageFile row
                ImageFile dbFolderImageFile;
                for (int i = 0; i < physcialFiles.Length; i++)
                {
                    physcialFileName   = physcialFiles[i];
                    physcialFileLinkId = physcialFileName.Substring(physcialFileName.LastIndexOf("_") + 1, 36);
                    dbFolderImageFile  = dbFolderImageFiles.Where(f => f.Id == physcialFileLinkId).FirstOrDefault();
                    if (dbFolderImageFile == null)
                    {
                        ImageFile dbMisplacedImageFile;
                        using (var db = new OggleBoobleMySqlContext())
                        {
                            dbMisplacedImageFile = db.ImageFiles.Where(m => m.Id == physcialFileLinkId).FirstOrDefault();
                        }
                        if (dbMisplacedImageFile != null)
                        {
                            if (dbMisplacedImageFile.FolderId == -6)
                            {
                                // should have moved file to rejects
                                //string newFileName = ftpPath + "/" + dbCategoryFolder.FolderPath + "/" + physcialFileName;
                                ftpFileName  = ftpPath + "/" + physcialFileName;
                                rejectFolder = ftpHost + "/archive.OggleBooble.com/rejects/" + dbMisplacedImageFile.FileName;
                                if (FtpUtilies.MoveFile(ftpFileName, rejectFolder) == "ok")
                                {
                                    using (var db = new OggleBoobleMySqlContext())
                                    {
                                        dbRejCatlinks = db.CategoryImageLinks.Where(l => l.ImageLinkId == dbMisplacedImageFile.Id).ToList();
                                        if (dbRejCatlinks.Count > 0)
                                        {
                                            foreach (CategoryImageLink dbRejCatlink in dbRejCatlinks)
                                            {
                                                if (dbRejCatlink.ImageCategoryId != -6)
                                                {
                                                    db.CategoryImageLinks.Remove(dbRejCatlink);
                                                    repairReport.CatLinksRemoved++;
                                                }
                                            }
                                            db.SaveChanges();
                                        }
                                    }
                                    repairReport.ImageFilesMoved++;
                                }
                            }
                            else
                            {
                                //var catLinks = db.CategoryImageLinks.Where(l => l.ImageLinkId == physcialFileLinkId).ToList();
                                bool itsok = false;
                                foreach (CategoryImageLink catLink in catLinks)
                                {
                                    if (catLink.ImageCategoryId == dbMisplacedImageFile.FolderId)
                                    {
                                        itsok = true;
                                        break;
                                    }
                                }
                                if (!itsok)
                                {
                                    using (var db = new OggleBoobleMySqlContext())
                                    {
                                        dbMisplacedImageFile.FolderId = folderId;
                                        db.SaveChanges();
                                        repairReport.ImageFilesMoved++;
                                    }
                                }
                            }
                        }  //  dbMisplacedImageFile != null
                        else
                        {
                            string            newFileName   = imgRepo + "/" + imageFolderPath + "/" + physcialFileName;
                            ImageFileInfo     imageFileInfo = GetImageFileInfo(newFileName);
                            CategoryImageLink ktest;
                            using (var db = new OggleBoobleMySqlContext())
                            {
                                ImageFile imageFile = new ImageFile()
                                {
                                    Id           = physcialFileLinkId,
                                    Acquired     = DateTime.Now,
                                    ExternalLink = "??",
                                    FileName     = physcialFileName,
                                    FolderId     = folderId,
                                    Height       = imageFileInfo.Height,
                                    Size         = imageFileInfo.Size,
                                    Width        = imageFileInfo.Width
                                };
                                db.ImageFiles.Add(imageFile);
                                dbFolderImageFiles.Add(imageFile);
                                db.SaveChanges();
                                repairReport.ImageFilesAdded++;
                                ktest = db.CategoryImageLinks.Where(l => (l.ImageCategoryId == folderId) && (l.ImageLinkId == physcialFileLinkId)).FirstOrDefault();
                            }
                            if (ktest == null)
                            {
                                try
                                {
                                    using (var db = new OggleBoobleMySqlContext())
                                    {
                                        int nextSortOrder = 0;
                                        if (db.CategoryImageLinks.Where(l => l.ImageCategoryId == folderId).Count() > 0)
                                        {
                                            nextSortOrder = db.CategoryImageLinks.Where(l => l.ImageCategoryId == folderId).Max(l => l.SortOrder) + 2;
                                        }
                                        db.CategoryImageLinks.Add(new CategoryImageLink()
                                        {
                                            ImageCategoryId = folderId,
                                            ImageLinkId     = physcialFileLinkId,
                                            SortOrder       = nextSortOrder
                                        });
                                        db.SaveChanges();
                                        repairReport.CatLinksAdded++;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    repairReport.Errors.Add("ktest lied: " + Helpers.ErrorDetails(ex));
                                }
                            }
                            else
                            {
                                if (dbFolderImageFile.Size == 0)
                                {
                                    //string newFileName = imgRepo + "/" + imageFolderPath + "/" + physcialFileName;
                                    //ImageFileInfo imageFileInfo2 = GetImageFileInfo(newFileName);
                                    if (imageFileInfo.Size == 0)
                                    {
                                        if (FtpUtilies.DeleteFile(ftpPath + "/" + dbFolderImageFile.FileName) == "ok")
                                        {
                                            using (var db = new OggleBoobleMySqlContext())
                                            {
                                                db.CategoryImageLinks.RemoveRange(db.CategoryImageLinks.Where(l => l.ImageLinkId == dbFolderImageFile.Id).ToList());
                                                db.ImageFiles.Remove(dbFolderImageFile);
                                                db.SaveChanges();
                                                repairReport.ZeroLenImageFilesRemoved++;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        using (var db = new OggleBoobleMySqlContext())
                                        {
                                            dbFolderImageFile.Size   = imageFileInfo.Size;
                                            dbFolderImageFile.Height = imageFileInfo.Height;
                                            dbFolderImageFile.Width  = imageFileInfo.Width;
                                            db.SaveChanges();
                                            repairReport.ZeroLenFileResized++;
                                        }
                                    }
                                }
                            }
                        }
                    } // dbFolderImageFile == null
                    repairReport.PhyscialFilesProcessed++;
                }
                #endregion

                #region 2. Make sure every ImageFile row has a physcial file
                // rebuild list
                var physcialFileLinkIds = new List <string>();

                for (int i = 0; i < physcialFiles.Length; i++)
                {
                    physcialFileLinkIds.Add(physcialFiles[i].Substring(physcialFiles[i].LastIndexOf("_") + 1, 36));
                }

                if (physcialFileLinkIds.Count() != dbFolderImageFiles.Count())
                {
                    if (physcialFileLinkIds.Count() > dbFolderImageFiles.Count())
                    {
                        using (var db = new OggleBoobleMySqlContext())
                        {
                            foreach (string pflinkId in physcialFileLinkIds)
                            {
                                dbFolderImageFile = db.ImageFiles.Where(i => i.Id == pflinkId).FirstOrDefault();
                                if (dbFolderImageFile == null)
                                {
                                    db.ImageFiles.Add(new ImageFile()
                                    {
                                        Id       = pflinkId,
                                        FolderId = folderId,
                                        FileName = ""
                                    });
                                    db.SaveChanges();
                                }
                                else if (dbFolderImageFile.FolderId != folderId)
                                {
                                    List <CategoryImageLink> links = db.CategoryImageLinks.Where(l => l.ImageLinkId == dbFolderImageFile.Id).ToList();
                                    if (links.Count() > 0)
                                    {
                                        dbFolderImageFile.FolderId = folderId;
                                        db.SaveChanges();
                                        repairReport.NoLinkImageFiles++;
                                    }
                                    else
                                    {
                                        string imageFileToRemove = ftpPath + "/" + imageFolderName + "_" + pflinkId + ".jpg";
                                        rejectFolder = ftpHost + "/archive.OggleBooble.com/rejects/" + imageFolderName + "_" + pflinkId + ".jpg";
                                        ftpSuccess   = FtpUtilies.MoveFile(imageFileToRemove, rejectFolder);
                                        //.DeleteFile(ftpPath + "/" + imageFolderName + "_" + pflinkId + ".jpg");
                                        //ftpSuccess = FtpUtilies.DeleteFile(ftpPath + "/" + imageFolderName + "_" + pflinkId + ".jpg");
                                        if (ftpSuccess == "ok")
                                        {
                                            repairReport.ImageFilesRemoved++;
                                            repairReport.Errors.Add("I moved a file to rejects : " + imageFolderName + "_" + pflinkId);
                                        }
                                        else
                                        {
                                            repairReport.Errors.Add("I wanted to delete : " + imageFolderName + "_" + pflinkId);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                foreach (ImageFile imageFile in dbFolderImageFiles)
                {
                    if (!physcialFileLinkIds.Contains(imageFile.Id))
                    {
                        if (imageFile.ExternalLink.IndexOf("http") > -1)
                        {
                            // Download missing File
                            if (imageFile.FileName.LastIndexOf(".") < 2)
                            {
                                repairReport.Errors.Add("bad filename: " + imageFile.FileName + "folder: " + imageFile.FolderId);
                            }
                            else
                            {
                                string expectedFileName = imageFolderName + "_" + imageFile.Id + imageFile.FileName.Substring(imageFile.FileName.LastIndexOf("."));
                                string newFileName      = imgRepo + "/" + imageFolderPath + "/" + expectedFileName;
                                string downLoadSuccess  = DownLoadImage(ftpPath, imageFile.ExternalLink, expectedFileName);
                                if (downLoadSuccess == "ok")
                                {
                                    repairReport.ImagesDownLoaded++;

                                    using (var db = new OggleBoobleMySqlContext())
                                    {
                                        if (db.CategoryImageLinks.Where(l => l.ImageLinkId == imageFile.Id).FirstOrDefault() == null)
                                        {
                                            db.CategoryImageLinks.Add(new CategoryImageLink()
                                            {
                                                ImageCategoryId = folderId, ImageLinkId = imageFile.Id, SortOrder = 9999
                                            });
                                            db.SaveChanges();
                                        }
                                    }
                                }
                                else
                                {
                                    //repairReport.Errors.Add("download faild: " + imageFile.ExternalLink);
                                    using (var db = new OggleBoobleMySqlContext())
                                    {
                                        if (db.CategoryImageLinks.Where(l => l.ImageLinkId == imageFile.Id).Count() > 0)
                                        {
                                            db.CategoryImageLinks.RemoveRange(db.CategoryImageLinks.Where(l => l.ImageLinkId == imageFile.Id).ToList());
                                            db.SaveChanges();
                                        }
                                        var ulIm = db.ImageFiles.Where(i => i.Id == imageFile.Id).FirstOrDefault();
                                        if (ulIm != null)
                                        {
                                            db.ImageFiles.Remove(ulIm);
                                            db.SaveChanges();
                                            repairReport.ImageFilesRemoved++;
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            repairReport.Errors.Add("ImageFile with no physcial file " + imageFile.Id);
                            using (var db = new OggleBoobleMySqlContext())
                            {
                                if (db.CategoryImageLinks.Where(l => l.ImageLinkId == imageFile.Id).Count() > 0)
                                {
                                    db.CategoryImageLinks.RemoveRange(db.CategoryImageLinks.Where(l => l.ImageLinkId == imageFile.Id).ToList());
                                }
                                var ulIm = db.ImageFiles.Where(i => i.Id == imageFile.Id).FirstOrDefault();
                                if (ulIm != null)
                                {
                                    db.ImageFiles.Remove(ulIm);
                                    db.SaveChanges();
                                    repairReport.ImageFilesRemoved++;
                                }
                            }
                        }
                    }
                    repairReport.ImageFilesProcessed++;
                }
                #endregion

                #region 3. check if there is a catlink for every physcial file

                using (var db = new OggleBoobleMySqlContext())
                {
                    foreach (string pfLinkId in physcialFileLinkIds)
                    {
                        if (db.CategoryImageLinks.Where(l => l.ImageCategoryId == folderId && l.ImageLinkId == pfLinkId).FirstOrDefault() == null)
                        {
                            int nextSortOrder = 0;
                            if (db.CategoryImageLinks.Where(l => l.ImageCategoryId == folderId).Count() > 0)
                            {
                                nextSortOrder = db.CategoryImageLinks.Where(l => l.ImageCategoryId == folderId).Max(l => l.SortOrder) + 2;
                            }
                            db.CategoryImageLinks.Add(new CategoryImageLink()
                            {
                                ImageCategoryId = folderId,
                                ImageLinkId     = pfLinkId,
                                SortOrder       = nextSortOrder
                            });
                            db.SaveChanges();
                            repairReport.CatLinksAdded++;
                        }
                        repairReport.LinkRecordsProcessed++;
                    }
                }
                #endregion

                repairReport.Success = "ok";
            }
            catch (Exception ex)
            {
                repairReport.Errors.Add(Helpers.ErrorDetails(ex));
            }
            if (recurr)
            {
                //var childFolders = db.CategoryFolders.Where(c => c.Parent == folderId).ToList();
                foreach (CategoryFolder childFolder in childFolders)
                {
                    PerformFolderChecks(childFolder.Id, repairReport, recurr);
                }
            }
        }
Exemplo n.º 3
0
        private string FtpMove(string currentFileName, MoveCopyImageModel model)
        {
            string success;
            string linkId = model.LinkId;

            using (var db = new OggleBoobleMySqlContext())
            {
                ImageFile dbImageFile = db.ImageFiles.Where(i => i.Id == linkId).FirstOrDefault();
                if (dbImageFile == null)
                {
                    return("link [" + linkId + "] not found");
                }
                //imageLinkId = dbImageFile.Id;

                // ? physcially more and rename .jpg
                CategoryFolder dbSourceFolder      = db.CategoryFolders.Where(f => f.Id == model.SourceFolderId).First();
                CategoryFolder dbDestinationFolder = db.CategoryFolders.Where(f => f.Id == model.DestinationFolderId).First();
                //string currentFileName = dbImageFile.FileName;
                string newFileName        = dbSourceFolder.FolderName + "_" + linkId;
                string destinationFtpPath = ftpHost + dbDestinationFolder.RootFolder + ".ogglebooble.com/" + Helpers.GetParentPath(model.DestinationFolderId) + dbDestinationFolder.FolderName;
                string sourceFtpPath      = ftpHost + dbSourceFolder.RootFolder + ".ogglebooble.com/" + Helpers.GetParentPath(model.SourceFolderId) + newFileName;

                if (!FtpUtilies.DirectoryExists(destinationFtpPath))
                {
                    FtpUtilies.CreateDirectory(destinationFtpPath);
                }

                success = FtpUtilies.MoveFile(sourceFtpPath + "/" + dbImageFile.FileName, destinationFtpPath + "/" + newFileName);
                if (success == "ok")
                {
                    #region move file on local drive
                    string localDestinationPath = "";
                    try
                    {
                        string localServerPath = "F:/Danni/";
                        string localSourcePath = localServerPath + Helpers.GetLocalParentPath(model.SourceFolderId) + dbSourceFolder.FolderName + "/" + dbImageFile.FileName;
                        //dbSourceFolder.FolderName + "_" + dbImageLink.Id + extension;
                        localDestinationPath = localServerPath + Helpers.GetLocalParentPath(model.DestinationFolderId) + dbDestinationFolder.FolderName;
                        FileInfo      localFile     = new FileInfo(localSourcePath);
                        DirectoryInfo directoryInfo = new DirectoryInfo(localDestinationPath);
                        if (!directoryInfo.Exists)
                        {
                            directoryInfo.Create();
                        }
                        localFile.MoveTo(localDestinationPath + "/" + newFileName);
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine("move file on local drive : " + Helpers.ErrorDetails(ex) + " " + localDestinationPath);
                    }
                    #endregion

                    //2. update ImageFile
                    //string linkPrefix = "http://" + dbDestinationFolder.RootFolder + ".ogglebooble.com/";
                    //newInternalLink = linkPrefix + Helpers.GetParentPath(model.DestinationFolderId) + dbDestinationFolder.FolderName + "/" + newFileName;
                    //var goDaddyrow = db.ImageLinks.Where(g => g.Id == dbImageLink.Id).FirstOrDefault();
                    var oldCatImageLink = db.CategoryImageLinks
                                          .Where(c => c.ImageCategoryId == model.SourceFolderId && c.ImageLinkId == dbImageFile.Id).First();
                    db.CategoryImageLinks.Add(new MySqlDataContext.CategoryImageLink()
                    {
                        ImageCategoryId = model.DestinationFolderId,
                        ImageLinkId     = dbImageFile.Id,
                        SortOrder       = oldCatImageLink.SortOrder
                    });
                    db.SaveChanges();
                    if (model.Mode == "Move")
                    {
                        db.CategoryImageLinks.Remove(oldCatImageLink);
                    }
                    db.SaveChanges();
                }
                return(success);
            }
        }
Exemplo n.º 4
0
        public string MoveLink(string linkId, int destinationFolderId, string request)
        {
            string success;

            try
            {
                string ftpRepo = imgRepo.Substring(7);
                using (var db = new OggleBoobleMySqlContext())
                {
                    ImageFile dbImageFile = db.ImageFiles.Where(i => i.Id == linkId).First();
                    string    ext         = dbImageFile.FileName.Substring(dbImageFile.FileName.LastIndexOf("."));

                    CategoryFolder dbSourceFolder = db.CategoryFolders.Where(f => f.Id == dbImageFile.FolderId).First();
                    string         sourceFtpPath  = ftpHost + ftpRepo + "/" + dbSourceFolder.FolderPath;
                    string         fileName       = dbImageFile.FileName;
                    //string assumedFileName = dbSourceFolder.FolderName + "_" + linkId + ext;

                    CategoryFolder dbDestFolder = db.CategoryFolders.Where(i => i.Id == destinationFolderId).First();
                    string         destFtpPath  = ftpHost + ftpRepo + "/" + dbDestFolder.FolderPath;

                    string newFileName = dbDestFolder.FolderName + "_" + linkId + ext;
                    if (dbSourceFolder.Id == dbDestFolder.Parent)
                    {
                        newFileName = dbSourceFolder.FolderName + "_" + linkId + ext;
                    }

                    if (!FtpUtilies.DirectoryExists(destFtpPath))
                    {
                        FtpUtilies.CreateDirectory(destFtpPath);
                    }

                    success = FtpUtilies.MoveFile(sourceFtpPath + "/" + fileName, destFtpPath + "/" + newFileName);

                    if (success == "ok")
                    {  // update ImageFile
                        dbImageFile.FileName = newFileName;
                        dbImageFile.FolderId = destinationFolderId;

                        // create new link
                        db.CategoryImageLinks.Add(new MySqlDataContext.CategoryImageLink()
                        {
                            ImageCategoryId = destinationFolderId,
                            ImageLinkId     = linkId,
                            SortOrder       = 1746
                        });

                        if (request == "MOV")
                        {  // update link
                            MySqlDataContext.CategoryImageLink existinglink = db.CategoryImageLinks
                                                                              .Where(l => l.ImageLinkId == linkId && l.ImageCategoryId == dbSourceFolder.Id).First();
                            db.CategoryImageLinks.Remove(existinglink);
                        }
                        //if (request == "ARK")


                        db.SaveChanges();
                        success = "ok";
                    }
                }
            }
            catch (Exception ex) { success = Helpers.ErrorDetails(ex); }
            return(success);
        }
Exemplo n.º 5
0
        public string MoveMany(MoveManyModel moveManyModel)
        {
            string success = "";

            try
            {
                using (var db = new OggleBoobleMySqlContext())
                {
                    try
                    {
                        string ftpRepo      = imgRepo.Substring(7);
                        var    dbDestFolder = db.CategoryFolders.Where(i => i.Id == moveManyModel.DestinationFolderId).First();
                        string destFtpPath  = ftpHost + ftpRepo + "/" + dbDestFolder.FolderPath;

                        if (!FtpUtilies.DirectoryExists(destFtpPath))
                        {
                            FtpUtilies.CreateDirectory(destFtpPath);
                        }

                        var    dbSourceFolder = db.CategoryFolders.Where(f => f.Id == moveManyModel.SourceFolderId).First();
                        string sourceFtpPath  = ftpHost + ftpRepo + "/" + dbSourceFolder.FolderPath;

                        ImageFile dbImageFile = null;
                        string    oldFileName;
                        string    newFileName;
                        string    linkId;
                        int       sortOrder;
                        for (int i = 0; i < moveManyModel.ImageLinkIds.Length; i++)
                        {
                            linkId = moveManyModel.ImageLinkIds[i];
                            if (moveManyModel.Context == "copy") //only
                            {
                                db.CategoryImageLinks.Add(new MySqlDataContext.CategoryImageLink()
                                {
                                    ImageCategoryId = dbDestFolder.Id,
                                    ImageLinkId     = linkId,
                                    SortOrder       = 9876
                                });
                                db.SaveChanges();
                            }
                            else
                            {
                                dbImageFile = db.ImageFiles.Where(f => f.Id == linkId).First();
                                oldFileName = dbImageFile.FileName;
                                string ext = dbImageFile.FileName.Substring(dbImageFile.FileName.LastIndexOf("."));

                                if (dbDestFolder.FolderType == "singleChild")
                                {
                                    var destinationParent = db.CategoryFolders.Where(f => f.Id == dbDestFolder.Parent).First();
                                    newFileName = destinationParent.FolderName + "_" + linkId + ext;
                                }
                                else
                                {
                                    newFileName = dbDestFolder.FolderName + "_" + linkId + ext;
                                }

                                if (dbDestFolder.Parent == dbSourceFolder.Id)
                                {
                                    newFileName = oldFileName;
                                }

                                success = FtpUtilies.MoveFile(sourceFtpPath + "/" + oldFileName, destFtpPath + "/" + newFileName);
                                if (success == "ok")
                                {
                                    dbImageFile.FolderId = moveManyModel.DestinationFolderId;
                                    dbImageFile.FileName = newFileName;
                                    db.SaveChanges();

                                    var oldLink = db.CategoryImageLinks.Where(l => l.ImageCategoryId == dbSourceFolder.Id && l.ImageLinkId == linkId).First();
                                    sortOrder = oldLink.SortOrder;
                                    if (moveManyModel.Context == "move")
                                    {
                                        db.CategoryImageLinks.Remove(oldLink);
                                        db.SaveChanges();
                                    }

                                    db.CategoryImageLinks.Add(new CategoryImageLink()
                                    {
                                        ImageCategoryId = dbDestFolder.Id,
                                        ImageLinkId     = linkId,
                                        SortOrder       = sortOrder
                                    });
                                    db.SaveChanges();
                                    // SIGNAR
                                }
                                else
                                {
                                    return(success);
                                }
                            }
                        }
                        db.SaveChanges();
                        success = "ok";
                    }
                    catch (Exception ex)
                    {
                        success = Helpers.ErrorDetails(ex);
                        if (db != null)
                        {
                            db.Dispose();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                success += Helpers.ErrorDetails(ex);
            }
            return(success);
        }