public async Task <IActionResult> UploadUrl(string url, string album = "19") { if (!DB.Keys.TryGetValue(Request.Headers["Authorization"][0], out ApiUser User)) { return(Unauthorized()); } if (!int.TryParse(album, out int albumid) || !DB.Albums.TryGetValue(albumid, out GAlbum GA)) { return(BadRequest("Invalid album id")); } if (User.ID != "190590364871032834") { if (GA.owner == User.ID) { } else if (GA.access.TryGetValue(User.ID, out GAlbum.GalleryAccess access)) { if (access == GAlbum.GalleryAccess.Read || access == GAlbum.GalleryAccess.ReadAPI) { return(Unauthorized()); } } else { return(Unauthorized()); } } if (string.IsNullOrEmpty(url) || !url.StartsWith("https://")) { return(BadRequest()); } byte[] Bytes = null; try { Bytes = await Program.Http.GetByteArrayAsync(url); } catch (Exception ex) { return(InternalServerError(ex.Message)); } if (Bytes.Length == 0) { return(BadRequest("Invalid image")); } string ImageName = url.Split('/').Last().Split('.').First(); MagickFormat Format = MagickFormat.A; string FormatName = url.Split('.').Last().Split('?').First().ToLower(); string Hash = Program.getMd5Hash(Bytes); if (album == "19") { if (DB.Images.Values.Any(x => x.album == 19 && x.file.hash == Hash)) { return(CustomStatus(409, "Already exist.")); } } else { if (DB.HashSet.ContainsKey(Hash)) { return(CustomStatus(409, "Already exist.")); } } string ID = Program.Gen.CreateId().ToString(); bool GifConvert = false; switch (FormatName) { case "png": Format = MagickFormat.Png; break; case "jpg": case "jpeg": Format = MagickFormat.Jpg; break; case "webp": Format = MagickFormat.WebP; break; case "gif": Format = MagickFormat.Gif; if (album == "19") { GifConvert = true; FormatName = "png"; } else { using (FileStream files = new FileStream(Config.GlobalPath + "img/" + ID + ".gif", FileMode.Create, FileAccess.Write)) { files.Write(Bytes); } } break; } try { DB.HashSet.Add(Hash, ID); } catch { } int Width = 0; int Height = 0; using (MagickImage image = new MagickImage(new MemoryStream(Bytes), new MagickReadSettings { ColorType = ColorType.Optimize, Format = Format, FrameIndex = 0, FrameCount = 1 })) { image.Strip(); Width = image.Width; Height = image.Height; if (GifConvert || Format != MagickFormat.Gif) { image.Write(Config.GlobalPath + "img/" + ID + "." + FormatName, Format); } image.Resize(792, 594); image.Write(Config.GlobalPath + "med/" + ID + ".webp", Format); //image.Resize(320, 320); //image.Write(Config.GlobalPath + "thm/" + ID + "." + FormatName, Format); } GImage img = new GImage { id = ID, name = ImageName, author = "190590364871032834", date = DateTime.UtcNow, file = new GImage.FileInfo { hash = Hash, height = Height, width = Width, size = Bytes.Length, type = FormatName }, album = albumid }; DB.Images.Add(ID, img); img.Add(); return(Ok()); }