/// <summary> /// Fonction pour trouver le type de lien /// </summary> /// <param name="link">Nouveau lien</param> /// <param name="site">Site de l'élément source</param> /// <param name="local">Local de l'élément source</param> /// <param name="rack">Rack de l'élément source</param> /// <returns></returns> public AddLinkModel GetLinkType(AddLinkModel link, string site, string local, string rack) { var destination = _context.Elements.Include(e => e.Racks).ThenInclude(r => r.Local).ThenInclude(l => l.Site).Where(e => link.Destination_Element_id == e.Element_id).First(); if (site != destination.Racks.Local.Site.Name) // On vérifie si les noms de site/locaux/racks correspondent pour savoir si le lien est intra ou extra rack { if (!link.Source_Port_number.EndsWith(" IS", StringComparison.Ordinal)) // La convention est d'avoir IS/IL/IR à la fin de chaque liaison extra rack afin de pouvoir récupérer les routes correctent dans la classe Pathfinder { link.Source_Port_number += " IS"; link.Destination_Port_number += " IS"; } link.Link_type = LinkType.InterSite; } else if (local != destination.Racks.Local.Name) { if (!link.Source_Port_number.EndsWith(" IL", StringComparison.Ordinal)) { link.Source_Port_number += " IL"; link.Destination_Port_number += " IL"; } link.Link_type = LinkType.InterLocal; } else if (rack != destination.Racks.Name) { if (!link.Source_Port_number.EndsWith(" IR", StringComparison.Ordinal)) { link.Source_Port_number += " IR"; link.Destination_Port_number += " IR"; } link.Link_type = LinkType.InterRack; } return(link); }
public async Task <ActionResult> Post([FromBody] AddLinkModel model) { if (!Uri.TryCreate(model.Url, UriKind.Absolute, out _)) { return(BadRequest()); } if (!HttpContext.Request.Cookies.TryGetValue("userId", out var userId)) { userId = Guid.NewGuid().ToString(); HttpContext.Response.Cookies.Append("userId", userId); } var newLink = new Link { OriginalLink = model.Url, ShortLink = GenerateShortLink(), UserId = userId }; await _linkService.CreateLink(newLink); return(Ok(new { ShortLink = $"{Request.Host.Value}{Request.Path.Value}/{newLink.ShortLink}" })); }
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"; //} }