예제 #1
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);
        }
예제 #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);
                }
            }
        }
예제 #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);
            }
        }
예제 #4
0
        public SuccessModel AddImageLink(AddLinkModel addLinkModel)
        {
            SuccessModel successModel = new SuccessModel();

            try
            {
                string mySqlDestPath;
                string newFileName;
                string newLink     = addLinkModel.Link;
                string imageLinkId = Guid.NewGuid().ToString();
                string extension   = newLink.Substring(newLink.LastIndexOf("."));
                using (var db = new OggleBoobleMySqlContext())
                {
                    if (newLink.IndexOf("?") > 0)
                    {
                        newLink   = newLink.Substring(0, newLink.IndexOf("?"));
                        extension = newLink.Substring(newLink.LastIndexOf("."));
                    }

                    var existingLink = db.ImageFiles.Where(l => l.ExternalLink.Substring(l.ExternalLink.IndexOf(":")) == newLink.Substring(newLink.IndexOf(":"))).FirstOrDefault();
                    if (existingLink != null)
                    {
                        successModel.Success = "Link Already Added";
                        return(successModel);
                    }
                    var destinationFolder = db.CategoryFolders.Where(f => f.Id == addLinkModel.FolderId).First();
                    //mySqlDestPath = db.CategoryFolders.Where(f => f.Id == addLinkModel.FolderId).FirstOrDefault().FolderPath;
                    mySqlDestPath = destinationFolder.FolderPath;

                    if (destinationFolder.FolderType == "singleChild")
                    {
                        var destinationParent = db.CategoryFolders.Where(f => f.Id == destinationFolder.Parent).First();
                        newFileName = destinationParent.FolderName + "_" + imageLinkId + extension;
                    }
                    else
                    {
                        newFileName = destinationFolder.FolderName + "_" + imageLinkId + extension;
                    }
                }
                string appDataPath = System.Web.HttpContext.Current.Server.MapPath("~/App_Data/temp/");
                string trimPath    = addLinkModel.Path.Replace("/Root/", "").Replace("%20", " ");

                // USE WEBCLIENT TO CREATE THE FILE
                using (WebClient wc = new WebClient())
                {
                    try
                    {
                        wc.DownloadFile(new Uri(newLink), appDataPath + "tempImage" + extension);
                    }
                    catch
                    {
                        if (newLink.StartsWith("https"))
                        {
                            try
                            {
                                newLink = "http" + newLink.Substring(5);
                                wc.DownloadFile(new Uri(newLink), appDataPath + "tempImage" + extension);
                            }
                            catch (Exception ex)
                            {
                                successModel.Success = "wc. download didnt work " + ex.Message;
                                return(successModel);
                            }
                        }
                    }
                }
                // USE WEBREQUEST TO UPLOAD THE FILE
                FtpWebRequest webRequest = null;
                try
                {
                    // todo  write the image as a file to x.ogglebooble  4/1/19
                    //webRequest = (FtpWebRequest)WebRequest.Create(ftpPath + "/" + newFileName);
                    //webRequest.Credentials = networkCredentials;
                    //var zz = webRequest.Method = WebRequestMethods.Ftp.UploadFile;

                    //var mmDom = repoDomain.Substring(8);

                    string destPath = ftpHost + repoDomain.Substring(8) + "/" + mySqlDestPath;

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

                    webRequest             = (FtpWebRequest)WebRequest.Create(destPath + "/" + newFileName);
                    webRequest.Credentials = networkCredentials;
                    webRequest.Method      = WebRequestMethods.Ftp.UploadFile;
                }
                catch (Exception ex)
                {
                    successModel.Success = " webRequest didnt work " + ex.Message;
                    return(successModel);
                }
                // TAKE THE WEBREQUEST FILE STREAM TO
                try
                {
                    using (Stream requestStream = webRequest.GetRequestStream())
                    {
                        byte[] fileContents = System.IO.File.ReadAllBytes(appDataPath + "tempImage" + extension);
                        webRequest.ContentLength = fileContents.Length;
                        requestStream.Write(fileContents, 0, fileContents.Length);
                        requestStream.Flush();
                        requestStream.Close();
                    }
                }
                catch (Exception ex)
                {
                    successModel.Success = "GetRequestStream didn't work " + ex.Message;
                    return(successModel);
                }
                // turn the tempfile into a fileStream to get its size attributes
                int fWidth = 0; int fHeight = 0; long fSize = 0;
                try
                {
                    using (var fileStream = new FileStream(appDataPath + "tempImage" + extension, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        fSize = fileStream.Length;
                        using (var image = System.Drawing.Image.FromStream(fileStream, false, false))
                        {
                            fWidth  = image.Width;
                            fHeight = image.Height;
                        }
                    }
                    DirectoryInfo directory = new DirectoryInfo(appDataPath);
                    FileInfo      tempFile  = directory.GetFiles("tempImage" + extension).FirstOrDefault();
                    if (tempFile != null)
                    {
                        tempFile.Delete();
                    }
                    System.Diagnostics.Debug.WriteLine("delete worked ");
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("delete didn't work " + ex.Message);
                }
                using (var db = new OggleBoobleMySqlContext())
                {
                    db.ImageFiles.Add(new ImageFile()
                    {
                        Id           = imageLinkId,
                        FolderId     = addLinkModel.FolderId,
                        ExternalLink = newLink,
                        Width        = fWidth,
                        Height       = fHeight,
                        Size         = fSize,
                        Acquired     = DateTime.Now,
                        FileName     = newFileName
                    });

                    //int nextSortOrder = db.CategoryImageLinks.Where(l=>l.ImageCategoryId==addLinkModel.FolderId).Select(Math.Max(sort))

                    db.CategoryImageLinks.Add(new MySqlDataContext.CategoryImageLink()
                    {
                        ImageCategoryId = addLinkModel.FolderId,
                        ImageLinkId     = imageLinkId,
                        SortOrder       = 9996
                    });
                    db.SaveChanges();
                }
                successModel.Success = "ok";
            }
            catch (Exception ex) { successModel.Success = Helpers.ErrorDetails(ex); }
            return(successModel);
            // COPY FILE TO LOCAL ?
            //try
            //{
            //    string localRoot = dbCategory.RootFolder;
            //    if (localRoot == "centerfold")
            //        localRoot = "playboy";

            //    var test = newLink.Path.Remove(0, newLink.Path.IndexOf("/", newLink.Path.IndexOf("/") + 1));
            //    var localPath = localRepoPath + localRoot + ".OGGLEBOOBLE.COM" + newLink.Path.Remove(0, newLink.Path.IndexOf("/", newLink.Path.IndexOf("/") + 1));
            //    DirectoryInfo dirInfo = new DirectoryInfo(localPath);
            //    if (!dirInfo.Exists)
            //        dirInfo.Create();
            //    wc.DownloadFile(new Uri(newLink.Link), localPath + "/" + newFileName);
            //}
            //catch (Exception ex)
            //{
            //    var err = Helpers.ErrorDetails(ex);
            //    System.Diagnostics.Debug.WriteLine("wc. download didnt work " + err);
            //}

            // see if this is the first image in the folder. If so make it the folder image
            //try
            //{
            //    int lnkCount = db.CategoryImageLinks.Where(c => c.ImageCategoryId == newLink.FolderId).Count();
            //    if (lnkCount == 0)
            //        successModel.ReturnValue = imageLinkId;
            //    else
            //        successModel.ReturnValue = "0";
            //}
            //catch (Exception ex)
            //{
            //    successModel.Success = Helpers.ErrorDetails(ex);
            //    if (successModel.Success.StartsWith("ERROR: Cannot insert duplicate key row in object"))
            //        successModel.Success = "Alredy Added";
            //}
        }
예제 #5
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);
        }
예제 #6
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);
        }