public static string MoveFolder(string source, string destination) { string success = ""; try { if (!FtpUtilies.DirectoryExists(destination)) { FtpUtilies.CreateDirectory(destination); } FtpWebRequest requestDir = (FtpWebRequest)WebRequest.Create(source); requestDir.Credentials = networkCredentials; requestDir.UseBinary = true; requestDir.UsePassive = false; requestDir.KeepAlive = false; requestDir.Proxy = null; requestDir.Method = WebRequestMethods.Ftp.Rename; requestDir.RenameTo = destination; FtpWebResponse response = (FtpWebResponse)requestDir.GetResponse(); response.Close(); success = "ok"; } catch (Exception ex) { success = Helpers.ErrorDetails(ex); } return(success); }
public SuccessModel Create(int parentId, string newFolderName, string folderType) { SuccessModel successModel = new SuccessModel(); try { using (var db = new OggleBoobleMySqlContext()) { string ftpRepo = imgRepo.Substring(7); var dbDestParent = db.CategoryFolders.Where(i => i.Id == parentId).First(); string newFtpPath = ftpHost + ftpRepo + "/" + dbDestParent.FolderPath + "/" + newFolderName; if (FtpUtilies.DirectoryExists(newFtpPath)) { successModel.Success = "folder already exists"; return(successModel); } successModel.Success = FtpUtilies.CreateDirectory(newFtpPath); if (successModel.Success == "ok") { CategoryFolder newFolder = new CategoryFolder(); //newFolder.Id = newFolderId; newFolder.Parent = parentId; newFolder.FolderName = newFolderName.Trim(); newFolder.FolderType = folderType; newFolder.RootFolder = dbDestParent.RootFolder; newFolder.SortOrder = 0; newFolder.FolderPath = dbDestParent.FolderPath + "/" + newFolderName; db.CategoryFolders.Add(newFolder); db.SaveChanges(); int newFolderId = newFolder.Id; successModel.ReturnValue = newFolderId.ToString(); successModel.Success = "ok"; } } } catch (Exception ex) { successModel.Success = Helpers.ErrorDetails(ex); } return(successModel); }
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); } }
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"; //} }
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); }
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); }