public RepairReportModel EmergencyFolderLocationFix(int root) { RepairReportModel repairReportModel = new RepairReportModel(); EmergencyFolderLocationRecurr(root, repairReportModel); return(repairReportModel); }
private void VerifyFolderRows(int folderId, RepairReportModel repairReport, OggleBoobleMySqlContext db, bool recurr) { try { string ftpPath; var parentFolder = db.CategoryFolders.Where(f => f.Id == folderId).FirstOrDefault(); ftpPath = ftpHost + "/" + imgRepo.Substring(8) + "/" + parentFolder.FolderPath; var subFolders = db.CategoryFolders.Where(f => f.Parent == folderId).ToList(); foreach (CategoryFolder subFolder in subFolders) { ftpPath = ftpHost + "/" + imgRepo.Substring(8) + "/" + subFolder.FolderPath; string[] v = FtpUtilies.GetDirectories(ftpPath); foreach (string dirDetail in v) { string folderName = dirDetail.Substring(dirDetail.IndexOf("<DIR>") + 5).Trim(); if (db.CategoryFolders.Where(f => f.FolderName == folderName).FirstOrDefault() == null) { repairReport.Errors.Add(folderName + "not found"); System.Threading.Thread.Sleep(101); db.ErrorLogs.Add(new ErrorLog() { CalledFrom = "VerifyFolderRow", ErrorCode = "xxx", FolderId = subFolder.Id, ErrorMessage = "folder: " + folderName + " not found", Occured = DateTime.Now, VisitorId = "VFR" }); db.SaveChanges(); } repairReport.LinkRecordsProcessed++; } repairReport.CatLinksRemoved++; } if (recurr) { foreach (CategoryFolder f in subFolders) { VerifyFolderRows(f.Id, repairReport, db, recurr); } } repairReport.Success = "ok"; } catch (Exception ex) { repairReport.Success = Helpers.ErrorDetails(ex); throw; } }
private void VerifyFolderPaths(int folderId, RepairReportModel repairReport, OggleBoobleMySqlContext db, bool recurr) { try { string ftpPath; var parentFolder = db.CategoryFolders.Where(f => f.Id == folderId).FirstOrDefault(); var subFolders = db.CategoryFolders.Where(f => f.Parent == folderId).ToList(); foreach (CategoryFolder f in subFolders) { ftpPath = ftpHost + "/" + imgRepo.Substring(8) + "/" + f.FolderPath; if (!FtpUtilies.DirectoryExists(ftpPath)) { System.Threading.Thread.Sleep(1001); repairReport.Errors.Add(f.Id + ", " + f.FolderName + ", " + f.FolderPath); db.ErrorLogs.Add(new ErrorLog() { CalledFrom = "VerifyFolderPaths", ErrorCode = "x77", FolderId = f.Id, ErrorMessage = ftpPath, Occured = DateTime.Now, VisitorId = "XXX" }); } repairReport.PhyscialFilesProcessed++; } if (recurr) { foreach (CategoryFolder f in subFolders) { VerifyFolderPaths(f.Id, repairReport, db, recurr); } } repairReport.Success = "ok"; } catch (Exception ex) { repairReport.Success = Helpers.ErrorDetails(ex); throw; } }
public RepairReportModel RepairLinks(int folderId, bool recurr) { RepairReportModel repairReport = new RepairReportModel(); using (var db = new OggleBoobleMySqlContext()) { try { //VerifyFolderRow(folderId, repairReport, db, true); //VerifyFolderPaths(folderId, repairReport, db, true); PerformFolderChecks(folderId, repairReport, recurr); } catch (Exception ex) { repairReport.Success = Helpers.ErrorDetails(ex); } } return(repairReport); }
public RepairReportModel UpdateDates(int startFolderId) { RepairReportModel results = new RepairReportModel(); try { using (OggleBoobleContext db = new OggleBoobleContext()) { var timer = new System.Diagnostics.Stopwatch(); timer.Start(); UpDateDatesRecurr(startFolderId, results, db); timer.Stop(); System.Diagnostics.Debug.WriteLine("VerifyLinksRecurr took: " + timer.Elapsed); } results.Success = "ok"; } catch (Exception ex) { results.Success = Helpers.ErrorDetails(ex); } return(results); }
public RepairReportModel RepairLinks(int startFolderId, string drive) { RepairReportModel repairReport = new RepairReportModel() { isSubFolder = false }; try { using (OggleBoobleContext db = new OggleBoobleContext()) { var timer = new System.Diagnostics.Stopwatch(); timer.Start(); RepairLinksRecurr(startFolderId, repairReport, db); timer.Stop(); System.Diagnostics.Debug.WriteLine("VerifyLinksRecurr took: " + timer.Elapsed); } repairReport.Success = "ok"; } catch (Exception ex) { repairReport.Success = Helpers.ErrorDetails(ex); } return(repairReport); }
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); }
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); } } }
public RepairReportModel MoveManyCleanup(int root) { RepairReportModel repairReportModel = new RepairReportModel(); try { using (OggleBoobleContext db = new OggleBoobleContext()) { CategoryFolder startFolder = db.CategoryFolders.Where(f => f.Id == root).FirstOrDefault(); string ftpPath = ftpHost + "/" + startFolder.RootFolder + ".ogglebooble.com/" + Helpers.GetParentPath(startFolder.Id) + startFolder.FolderName; string[] rootFiles = FtpUtilies.GetFiles(ftpPath); //List<string> rootFileLinks = new List<string>(); //foreach (string fileName in rootFiles) rootFileLinks.Add(fileName.Substring(fileName.LastIndexOf("_") + 1, 36)); List <CategoryFolder> subDirs = db.CategoryFolders.Where(f => f.Parent == root).ToList(); string sourcePath = startFolder.RootFolder + ".ogglebooble.com/" + Helpers.GetParentPath(startFolder.Id) + startFolder.FolderName; string moveFileSuccess, newLink, linkId, destinationPath; string imageFileName = ""; foreach (CategoryFolder subDir in subDirs) { destinationPath = subDir.RootFolder + ".ogglebooble.com/" + Helpers.GetParentPath(subDir.Id) + subDir.FolderName; List <CategoryImageLink> subFolderLinks = db.CategoryImageLinks.Where(l => l.ImageCategoryId == subDir.Id).ToList(); foreach (CategoryImageLink subFolderLink in subFolderLinks) { bool found = false; foreach (string fileName in rootFiles) { linkId = fileName.Substring(fileName.LastIndexOf("_") + 1, 36); if (linkId == subFolderLink.ImageLinkId) { found = true; imageFileName = fileName; break; } } //if (rootFiles.Contains(subFolderLink.ImageLinkId)) if (found) { ImageLink imageLink = db.ImageLinks.Where(i => i.Id == subFolderLink.ImageLinkId).FirstOrDefault(); string ext = imageFileName.Substring(imageFileName.LastIndexOf(".")); string newImageFileName = subDir.FolderName + "_" + subFolderLink.ImageLinkId + imageFileName.Substring(imageFileName.LastIndexOf(".")); if (FtpUtilies.DirectoryExists(ftpHost + sourcePath + "/" + imageFileName)) { moveFileSuccess = FtpUtilies.MoveFile(ftpHost + sourcePath + "/" + imageFileName, ftpHost + destinationPath + "/" + newImageFileName); if (moveFileSuccess == "ok") { if (imageLink != null) { newLink = "http://" + destinationPath + "/" + newImageFileName; imageLink.Link = newLink; imageLink.FolderLocation = subDir.Id; db.SaveChanges(); repairReportModel.LinksEdited++; CategoryImageLink badCategoryImageLink = db.CategoryImageLinks .Where(c => c.ImageLinkId == subFolderLink.ImageLinkId && c.ImageCategoryId == root).FirstOrDefault(); if (badCategoryImageLink != null) { db.CategoryImageLinks.Remove(badCategoryImageLink); db.SaveChanges(); repairReportModel.LinksRemoved++; } } else { repairReportModel.Errors.Add("no imagelink found for link " + subFolderLink.ImageLinkId + " folder: " + startFolder.FolderName); } } else { repairReportModel.Errors.Add("link " + ftpHost + sourcePath + imageFileName + " error: " + moveFileSuccess); } } } repairReportModel.RowsProcessed++; } } repairReportModel.Success = "ok"; } } catch (Exception ex) { repairReportModel.Success = Helpers.ErrorDetails(ex); } return(repairReportModel); }
private void EmergencyFolderLocationRecurr(int root, RepairReportModel repairReportModel) { try { using (OggleBoobleContext db = new OggleBoobleContext()) { List <CategoryFolder> categoryFolders = db.CategoryFolders.Where(f => f.Parent == root).ToList(); string linkId; foreach (CategoryFolder categoryFolder in categoryFolders) { string ftpPath = ftpHost + "/" + categoryFolder.RootFolder + ".ogglebooble.com/" + Helpers.GetParentPath(categoryFolder.Id) + categoryFolder.FolderName; string[] files = FtpUtilies.GetFiles(ftpPath); foreach (string fileName in files) { if (fileName.LastIndexOf("_") > 0) { linkId = fileName.Substring(fileName.LastIndexOf("_") + 1, 36); ImageLink imageLink = db.ImageLinks.Where(l => l.Id == linkId).FirstOrDefault(); if (imageLink != null) { if (imageLink.FolderLocation != categoryFolder.Id) { if (imageLink.FolderLocation == 4267) { imageLink.FolderLocation = categoryFolder.Id; db.SaveChanges(); repairReportModel.LinksEdited++; } } else { repairReportModel.ImagesMoved++; if (imageLink.FolderLocation == 4267) { Console.WriteLine("imageLink.FolderLocation() != categoryFolder.Id()"); } } } else { repairReportModel.Errors.Add("no imagelink found for link " + linkId + " folder: " + categoryFolder.FolderName); } } else { Console.WriteLine("bad filename?: " + fileName); repairReportModel.Errors.Add("bad filename ?: " + fileName); } repairReportModel.RowsProcessed++; } EmergencyFolderLocationRecurr(categoryFolder.Id, repairReportModel); } repairReportModel.Success = "ok"; } } catch (Exception ex) { repairReportModel.Success = Helpers.ErrorDetails(ex); } }
private void RepairLinksRecurr(int folderId, RepairReportModel repairReport, OggleBoobleContext db) { try { CategoryFolder dbCategoryFolder = db.CategoryFolders.Where(f => f.Id == folderId).First(); string rootFolder = dbCategoryFolder.RootFolder; if (rootFolder == "centerfold") { rootFolder = "playboy"; } string ftpPath = ftpHost + "/" + rootFolder + ".ogglebooble.com/" + Helpers.GetParentPath(folderId) + dbCategoryFolder.FolderName; if (!FtpUtilies.DirectoryExists(ftpPath)) { repairReport.DirNotFound++; FtpUtilies.CreateDirectory(ftpPath); repairReport.Errors.Add("created directory " + ftpPath); } int folderRowsProcessed = 0; //if (repairReport.isSubFolder) // SignalRHost.ProgressHub.PostToClient("Processing: " + dbCategoryFolder.FolderName + " Rows: " + folderRowsProcessed + " Total: " + repairReport.RowsProcessed); //else // SignalRHost.ProgressHub.PostToClient("Processing: " + dbCategoryFolder.FolderName + " Rows: " + folderRowsProcessed); List <ImageLink> goDaddyLinks = (from c in db.CategoryImageLinks join g in db.ImageLinks on c.ImageLinkId equals g.Id where c.ImageCategoryId == folderId && g.Link.Contains(dbCategoryFolder.FolderName) && g.FolderLocation == dbCategoryFolder.Id select(g)).ToList(); string[] imageFiles = FtpUtilies.GetFiles(ftpPath); string goDaddyPrefix = "http://" + rootFolder + ".ogglebooble.com/"; string expectedLinkName = goDaddyPrefix + Helpers.GetParentPath(folderId) + dbCategoryFolder.FolderName; string ext = ""; bool fileNameInExpectedForm; string linkId = ""; foreach (string fileName in imageFiles) { //EnsureCorrectFileName(fileName, ftpPath); if ((fileName.LastIndexOf("_") > 0) && (fileName.Substring(fileName.LastIndexOf("_")).Length > 40)) { fileNameInExpectedForm = true; if (fileName.IndexOf(".") > 0) { ext = fileName.Substring(fileName.Length - 4); linkId = fileName.Substring(fileName.LastIndexOf("_") + 1, 36); //expectedFileName = dbCategoryFolder.FolderName + "_" + linkId + ext; //if (fileName == expectedFileName) var folderNameWhereImageSayItShouldBe = fileName.Substring(0, fileName.IndexOf("_")); if (folderNameWhereImageSayItShouldBe != dbCategoryFolder.FolderName) { CategoryFolder categoryFolderWhereImageSayItShouldBe = db.CategoryFolders.Where(f => f.FolderName == folderNameWhereImageSayItShouldBe).FirstOrDefault(); if (categoryFolderWhereImageSayItShouldBe != null) { string ftpPathWhereImageSayItShouldBe = ftpHost + categoryFolderWhereImageSayItShouldBe.RootFolder + ".ogglebooble.com/" + Helpers.GetParentPath(categoryFolderWhereImageSayItShouldBe.Id) + categoryFolderWhereImageSayItShouldBe.FolderName; string[] ArrayWhereImageSayItShouldBe = FtpUtilies.GetFiles(ftpPathWhereImageSayItShouldBe); if (ArrayWhereImageSayItShouldBe.Contains(categoryFolderWhereImageSayItShouldBe.FolderName + "_" + linkId + ext)) { FtpUtilies.DeleteFile(ftpPath + categoryFolderWhereImageSayItShouldBe.FolderName + "_" + linkId + ext); repairReport.LinksRemoved++; } } else { // move file string source = ftpPath + "/" + fileName; string destination = ftpHost + categoryFolderWhereImageSayItShouldBe.RootFolder + ".ogglebooble.com/" + Helpers.GetParentPath(categoryFolderWhereImageSayItShouldBe.Id) + categoryFolderWhereImageSayItShouldBe.FolderName; if (source != destination) { FtpUtilies.MoveFile(source, destination); ImageLink extraLink = db.ImageLinks.Where(g => g.Id == linkId).FirstOrDefault(); if (extraLink != null) { db.ImageLinks.Remove(extraLink); db.SaveChanges(); } repairReport.ImagesMoved++; } } } ImageLink goDaddyLinkExists = db.ImageLinks.Where(g => g.Id == linkId).FirstOrDefault(); if (goDaddyLinkExists == null) { if (FtpUtilies.DirectoryExists(ftpPath + "/" + fileName)) { ImageLink newLink = new ImageLink() { Id = linkId, Link = expectedLinkName + "/" + fileName, ExternalLink = "unknown", FolderLocation = folderId }; db.ImageLinks.Add(newLink); db.SaveChanges(); repairReport.NewLinksAdded++; } } else { // if (goDaddyLinkExists.Link.ToUpper() != (expectedLinkName + "/" + fileName).ToUpper()) if (goDaddyLinkExists.Link != expectedLinkName + "/" + fileName) { goDaddyLinkExists.Link = expectedLinkName + "/" + fileName; db.SaveChanges(); repairReport.LinksEdited++; } } if (db.CategoryImageLinks.Where(c => c.ImageCategoryId == folderId).Where(c => c.ImageLinkId == linkId).Where(c => c.ImageCategoryId == folderId).FirstOrDefault() == null) { CategoryImageLink newCatLink = new CategoryImageLink() { ImageCategoryId = folderId, ImageLinkId = linkId }; db.CategoryImageLinks.Add(newCatLink); db.SaveChanges(); repairReport.CatLinksAdded++; } } else { repairReport.Errors.Add("extension problem"); fileNameInExpectedForm = false; } } else { fileNameInExpectedForm = false; ext = fileName.Substring(fileName.Length - 4); linkId = Guid.NewGuid().ToString(); //expectedFileName = dbCategoryFolder.FolderName + "_" + linkId + ext; } if (!fileNameInExpectedForm) { // rename file string renameSuccess = FtpUtilies.MoveFile(ftpPath + "/" + fileName, ftpPath + "/" + fileName); if (renameSuccess == "ok") { repairReport.ImagesRenamed++; ImageLink oldImageLink = goDaddyLinks.Where(g => g.Link == expectedLinkName + "/" + fileName).FirstOrDefault(); if (oldImageLink != null) { if (oldImageLink.Link != expectedLinkName + "/" + fileName) { // update godaddy link oldImageLink.Link = expectedLinkName + "/" + fileName; db.SaveChanges(); repairReport.LinksEdited++; } } else { // link not found in this folder's links ImageLink goDaddyLink = db.ImageLinks.Where(g => g.Id == linkId).FirstOrDefault(); if (goDaddyLink != null) { goDaddyLink.Link = expectedLinkName + "/" + fileName; db.SaveChanges(); repairReport.LinksEdited++; } else { var newImageLink = new ImageLink() { Id = linkId, FolderLocation = folderId, Link = expectedLinkName + "/" + fileName, ExternalLink = "" }; db.ImageLinks.Add(newImageLink); db.SaveChanges(); repairReport.NewLinksAdded++; } if (db.CategoryImageLinks.Where(c => c.ImageCategoryId == folderId).Where(c => c.ImageLinkId == linkId).FirstOrDefault() == null) { CategoryImageLink newCatLink = new CategoryImageLink() { ImageCategoryId = folderId, ImageLinkId = linkId }; db.CategoryImageLinks.Add(newCatLink); db.SaveChanges(); repairReport.CatLinksAdded++; } } } else { repairReport.Errors.Add("rename Failed: " + renameSuccess); } } //if (repairReport.isSubFolder) // SignalRHost.ProgressHub.PostToClient("Processing: " + dbCategoryFolder.FolderName + " Rows: " + folderRowsProcessed + " Total: " + repairReport.RowsProcessed); //else // SignalRHost.ProgressHub.PostToClient("Processing: " + dbCategoryFolder.FolderName + " Rows: " + folderRowsProcessed); repairReport.RowsProcessed++; folderRowsProcessed++; } if (goDaddyLinks.Count() != imageFiles.Count()) { if (goDaddyLinks.Count() > imageFiles.Count()) { string expectedFileName = ""; foreach (ImageLink goDaddyLink in goDaddyLinks) { expectedFileName = goDaddyLink.Link.Substring(goDaddyLink.Link.LastIndexOf("/") + 1); linkId = goDaddyLink.Link.Substring(goDaddyLink.Link.LastIndexOf("_") + 1, 36); if (!imageFiles.Contains(expectedFileName)) { ImageLink imageLink = db.ImageLinks.Where(i => i.Id == linkId).FirstOrDefault(); if (imageLink == null) { repairReport.Errors.Add("image file in folder with no imageLink row"); } else { if (imageLink.FolderLocation == folderId) { CategoryImageLink categoryImageLink = db.CategoryImageLinks.Where(l => l.ImageCategoryId == folderId && l.ImageLinkId == linkId).FirstOrDefault(); //if (categoryImageLink == null) { //repairReport.Errors.Add("image file in folder with no imageLink row"); List <ImageLink> flinks = db.ImageLinks.Where(i => i.ExternalLink == imageLink.ExternalLink).ToList(); string downLoadSuccess = DownLoadImage(ftpPath, goDaddyLink.ExternalLink, expectedFileName); if (downLoadSuccess == "ok") { repairReport.ImagesDownLoaded++; } else { // problem with links to child folder files repairReport.Errors.Add(goDaddyLink.ExternalLink + " " + downLoadSuccess); db.ImageLinks.Remove(goDaddyLink); var badCatFolderImageLink = db.CategoryImageLinks.Where(c => c.ImageCategoryId == dbCategoryFolder.Id && c.ImageLinkId == goDaddyLink.Id).FirstOrDefault(); if (badCatFolderImageLink != null) { db.CategoryImageLinks.Remove(badCatFolderImageLink); } db.SaveChanges(); repairReport.LinksRemoved++; } } } } } //if (repairReport.isSubFolder) // SignalRHost.ProgressHub.PostToClient("Processing: " + dbCategoryFolder.FolderName + " Rows: " + folderRowsProcessed + " Total: " + repairReport.RowsProcessed); //else // SignalRHost.ProgressHub.PostToClient("Processing: " + dbCategoryFolder.FolderName + " Rows: " + folderRowsProcessed); } } if (imageFiles.Count() > goDaddyLinks.Count()) { repairReport.Errors.Add("Extra Links Found in " + ftpPath); foreach (string imageFile in imageFiles) { linkId = imageFile.Substring(imageFile.IndexOf("_") - 1); var x = db.ImageLinks.Where(i => i.Link == imageFile).FirstOrDefault(); if (x == null) { // add a link db.ImageLinks.Add(new ImageLink() { Id = Guid.NewGuid().ToString(), Link = imageFile, ExternalLink = "gg" }); db.SaveChanges(); } } } } int[] subDirs = db.CategoryFolders.Where(f => f.Parent == folderId).Select(f => f.Id).ToArray(); foreach (int subDir in subDirs) { repairReport.isSubFolder = true; RepairLinksRecurr(subDir, repairReport, db); } } catch (Exception ex) { repairReport.Success = Helpers.ErrorDetails(ex); } }
private string UpDateDatesRecurr(int folderId, RepairReportModel results, OggleBoobleContext db) { string success = ""; try { //CategoryFolder dbCategoryFolder = db.CategoryFolders.Where(f => f.Id == folderId).FirstOrDefault(); //string ftpPath = ftpHost + "/" + dbCategoryFolder.RootFolder + ".ogglebooble.com/" + Helpers.GetParentPath(folderId); List <ImageLink> imageLinks = db.ImageLinks.Where(l => l.FolderLocation == folderId).ToList(); DateTime imageFileDateTime = DateTime.MinValue; int fWidth = 0; int fHeight = 0; long fSize = 0; string extension; string trimPath; string appDataPath = System.Web.HttpContext.Current.Server.MapPath("~/App_Data/temp/"); foreach (ImageLink imageLink in imageLinks) { if (imageLink.Width == null) { try { CategoryFolder dbCategory = db.CategoryFolders.Where(f => f.Id == imageLink.FolderLocation).First(); extension = imageLink.Link.Substring(imageLink.Link.LastIndexOf(".")); string newFileName = dbCategory.FolderName + "_" + imageLink.Id + extension; trimPath = imageLink.Link.Replace("/Root/", "").Replace("%20", " ").Replace(newFileName, "").Replace("http://", ""); // USE WEBCLIENT TO CREATE THE FILE using (WebClient wc = new WebClient()) { try { wc.DownloadFile(new Uri(imageLink.Link), appDataPath + "tempImage" + extension); } catch (Exception ex) { success = "wc. download didnt work " + ex.Message; } } FtpWebRequest webRequest = null; // USE WEBREQUEST TO UPLOAD THE FILE try { // todo write the image as a file to x.ogglebooble 4/1/19 string ftpPath = ftpHost + trimPath; if (!FtpUtilies.DirectoryExists(ftpPath)) { FtpUtilies.CreateDirectory(ftpPath); } webRequest = (FtpWebRequest)WebRequest.Create(ftpPath + "/" + newFileName); webRequest.Credentials = networkCredentials; webRequest.Method = WebRequestMethods.Ftp.UploadFile; //var zz = webRequest.Method = WebRequestMethods.Ftp.UploadFile; } catch (Exception ex) { success = " webRequest didnt work " + ex.Message; } // 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) { success = "GetRequestStream didn't work " + ex.Message; } 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; } imageLink.Width = fWidth; imageLink.Height = fHeight; db.SaveChanges(); } 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); } } if (imageLink.LastModified == null) { try { imageFileDateTime = FtpUtilies.GetLastModified(imageLink.Link.Replace("http://", ftpHost)); imageLink.LastModified = imageFileDateTime; db.SaveChanges(); results.NewLinksAdded++; } catch (Exception ex) { success = Helpers.ErrorDetails(ex); results.Errors.Add(imageLink.Id + " date: " + imageFileDateTime + " error: " + success); results.LinksRemoved++; } } else { results.ImagesRenamed++; } results.RowsProcessed++; } foreach (int subDirId in db.CategoryFolders.Where(f => f.Parent == folderId).Select(f => f.Id).ToArray()) { UpDateDatesRecurr(subDirId, results, db); } success = "ok"; } catch (Exception ex) { success = Helpers.ErrorDetails(ex); } return(success); }