public async Task AddNewTorrent(PostNewTorrent torrent) { var dict = new Dictionary <string, object>(); if ((torrent.Urls ?? new string[0]).Length > 0) { dict["urls"] = string.Join("\r\n", torrent.Urls); } if (!string.IsNullOrWhiteSpace(torrent.TorrentFilePath)) { var fileInfo = new FileInfo(torrent.TorrentFilePath); var stream = fileInfo.OpenRead(); dict["torrents"] = new MultipartFile(stream, fileInfo.Name); } if (torrent.SavePath.IsSpecified) { dict["savepath"] = torrent.SavePath.Value; } if (torrent.Cookie.IsSpecified) { dict["cookie"] = torrent.Cookie.Value; } if (torrent.Category.IsSpecified) { dict["category"] = torrent.Category.Value; } if (torrent.Tags.IsSpecified) { dict["tags"] = string.Join(",", torrent.Tags.Value); } if (torrent.SkipCheck.IsSpecified) { dict["skip_checking"] = torrent.SkipCheck.Value; } if (torrent.Paused.IsSpecified) { dict["paused"] = torrent.Paused.Value; } if (torrent.RootFolder.IsSpecified) { dict["root_folder"] = torrent.RootFolder.Value; } if (torrent.Rename.IsSpecified) { dict["rename"] = torrent.Rename.Value; } if (torrent.UploadLimit.IsSpecified) { dict["upLimit"] = torrent.UploadLimit.Value; } if (torrent.DownloadLimit.IsSpecified) { dict["dlLimit"] = torrent.DownloadLimit.Value; } if (torrent.Automatic.IsSpecified) { dict["autoTMM"] = torrent.Automatic.Value; } await SendMultipartAsync("POST", "torrents/add", dict); }
public Task <WebApiResponse> SetPhoto(MultipartFile image, int?squareSize = null) { return(_client.MakeMultiPartCall <WebApiResponse>("users.setPhoto", new Dictionary <string, string>().AddIfValue("crop_w", squareSize), new Dictionary <string, MultipartFile> { { "image", image } })); }
public IReadOnlyDictionary <string, object> ToDictionary() { var d = new Dictionary <string, object>(); d["file"] = new MultipartFile(File, Filename.GetValueOrDefault("unknown.dat")); if (Content.IsSpecified) { d["content"] = Content.Value; } if (IsTTS.IsSpecified) { d["tts"] = IsTTS.Value.ToString(); } if (Nonce.IsSpecified) { d["nonce"] = Nonce.Value; } if (Username.IsSpecified) { d["username"] = Username.Value; } if (AvatarUrl.IsSpecified) { d["avatar_url"] = AvatarUrl.Value; } return(d); }
public IReadOnlyDictionary <string, object> ToDictionary() { var d = new Dictionary <string, object>(); d["file"] = new MultipartFile(File, Filename.GetValueOrDefault("unknown.dat")); var payload = new Dictionary <string, object>(); if (Content.IsSpecified) { payload["content"] = Content.Value; } if (IsTTS.IsSpecified) { payload["tts"] = IsTTS.Value.ToString(); } if (Nonce.IsSpecified) { payload["nonce"] = Nonce.Value; } if (Embed.IsSpecified) { payload["embed"] = Embed.Value; } var json = new StringBuilder(); using (var text = new StringWriter(json)) using (var writer = new JsonTextWriter(text)) _serializer.Serialize(writer, payload); d["payload_json"] = json.ToString(); return(d); }
public IReadOnlyDictionary <string, object> ToDictionary() { var d = new Dictionary <string, object>(); int i = 0; foreach (var file in Files) { var filename = file.Key ?? "unknown.dat"; if (IsSpoiler && !filename.StartsWith(AttachmentExtensions.SpoilerPrefix)) { filename = filename.Insert(0, AttachmentExtensions.SpoilerPrefix); } d[$"file{i++}"] = new MultipartFile(file.Value, filename); } var payload = new Dictionary <string, object>(); if (Content.IsSpecified) { payload["content"] = Content.Value; } if (IsTTS.IsSpecified) { payload["tts"] = IsTTS.Value.ToString(); } if (Nonce.IsSpecified) { payload["nonce"] = Nonce.Value; } if (Username.IsSpecified) { payload["username"] = Username.Value; } if (AvatarUrl.IsSpecified) { payload["avatar_url"] = AvatarUrl.Value; } if (Embeds.IsSpecified) { payload["embeds"] = Embeds.Value; } if (AllowedMentions.IsSpecified) { payload["allowed_mentions"] = AllowedMentions.Value; } var json = new StringBuilder(); using (var text = new StringWriter(json)) using (var writer = new JsonTextWriter(text)) _serializer.Serialize(writer, payload); d["payload_json"] = json.ToString(); return(d); }
public IReadOnlyDictionary <string, object> ToDictionary() { Dictionary <string, object> d = new Dictionary <string, object>(); string filename = Filename.GetValueOrDefault("unknown.dat"); if (IsSpoiler && !filename.StartsWith(AttachmentExtensions.SpoilerPrefix)) { filename = filename.Insert(0, AttachmentExtensions.SpoilerPrefix); } d["file"] = new MultipartFile(File, filename); Dictionary <string, object> payload = new Dictionary <string, object>(); if (Content.IsSpecified) { payload["content"] = Content.Value; } if (IsTTS.IsSpecified) { payload["tts"] = IsTTS.Value.ToString(); } if (Nonce.IsSpecified) { payload["nonce"] = Nonce.Value; } if (Embed.IsSpecified) { payload["embed"] = Embed.Value; } if (AllowedMentions.IsSpecified) { payload["allowed_mentions"] = AllowedMentions.Value; } if (IsSpoiler) { payload["hasSpoiler"] = IsSpoiler.ToString(); } if (MessageReference.IsSpecified) { payload["message_reference"] = MessageReference.Value; } if (Components.IsSpecified) { payload["components"] = Components.Value; } StringBuilder json = new StringBuilder(); using (StringWriter text = new StringWriter(json)) using (JsonTextWriter writer = new JsonTextWriter(text)) _serializer.Serialize(writer, payload); d["payload_json"] = json.ToString(); return(d); }
public IReadOnlyDictionary <string, object> ToDictionary() { Dictionary <string, object> d = new Dictionary <string, object>(); Dictionary <string, object> data = new Dictionary <string, object>(); string filename = Data.Filename.GetValueOrDefault("unknown.dat"); if (Data.IsSpoiler && !filename.StartsWith(AttachmentExtensions.SpoilerPrefix)) { filename = filename.Insert(0, AttachmentExtensions.SpoilerPrefix); } d["file"] = new MultipartFile(Data.File, filename); d["type"] = (int)Type; Dictionary <string, object> payload = new Dictionary <string, object>(); if (Data.Content.IsSpecified) { payload["content"] = Data.Content.Value; } if (Data.IsTTS.IsSpecified) { payload["tts"] = Data.IsTTS.Value.ToString(); } if (Data.Nonce.IsSpecified) { payload["nonce"] = Data.Nonce.Value; } if (Data.Username.IsSpecified) { payload["username"] = Data.Username.Value; } if (Data.AvatarUrl.IsSpecified) { payload["avatar_url"] = Data.AvatarUrl.Value; } if (Data.Embeds.IsSpecified) { payload["embeds"] = Data.Embeds.Value; } if (Data.AllowedMentions.IsSpecified) { payload["allowed_mentions"] = Data.AllowedMentions.Value; } StringBuilder json = new StringBuilder(); using (StringWriter text = new StringWriter(json)) using (JsonTextWriter writer = new JsonTextWriter(text)) _serializer.Serialize(writer, payload); d["payload_json"] = json.ToString(); return(d); }
public Task <WebApiResponse> SetPhoto(MultipartFile image, int cropX, int cropY) { var dict = new Dictionary <string, string> { { "crop_x", cropX.ToString() }, { "crop_y", cropY.ToString() } }; return(_client.MakeMultiPartCall <WebApiResponse>("users.setPhoto", dict, new Dictionary <string, MultipartFile> { { "image", image } })); }
internal IReadOnlyDictionary <string, object> ToDictionary() { var d = new Dictionary <string, object>(); d["file"] = new MultipartFile(File, _filename.GetValueOrDefault("unknown.dat")); if (_content.IsSpecified) { d["content"] = _content.Value; } if (_isTTS.IsSpecified) { d["tts"] = _isTTS.Value.ToString(); } if (_nonce.IsSpecified) { d["nonce"] = _nonce.Value; } return(d); }
/// <summary> /// 验证文件大小,文件名,文件后缀 /// </summary> /// <param name="file"> </param> /// <param name="maxLength"> </param> /// <param name="allowExtName"> </param> public virtual void validateFile(MultipartFile file, long maxLength, string[] allowExtName) { if (file.Empty) { throw new FieldException("file", "您没有上传文件", null); } // 文件大小 if (file.Size < 0 || file.Size > maxLength) { throw new FieldException("file", "文件不允许超过" + maxLength.ToString(), null); } // // 处理不选择文件点击上传时,也会有MultipartFile对象,在此进行过滤 // string filename = file.OriginalFilename; if (string.ReferenceEquals(filename, "")) { throw new FieldException("file", "文件名不能为空", null); } // // 文件名后缀 // /* if (filename.contains(".")) { String extName = filename.substring(filename.lastIndexOf(".")).toLowerCase(); if (allowExtName == null || allowExtName.length == 0 || Arrays.binarySearch(allowExtName, extName) >= 0) { } else { //throw new FieldException("file", "文件后缀不允许", null); } } else { //throw new FieldException("file", "文件后缀不允许", null); }*/ }
public String storeFile(MultipartFile file) { // Normalize file name String fileName = StringUtils.cleanPath(file.getOriginalFilename()); try { // Check if the file's name contains invalid characters if (fileName.contains("..")) { throw new FileStorageException("Sorry! Filename contains invalid path sequence " + fileName); } // Copy file to the target location (Replacing existing file with the same name) Path targetLocation = this.fileStorageLocation.resolve(fileName); Files.copy(file.getInputStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING); return(fileName); } catch (IOException ex) { throw new FileStorageException("Could not store file " + fileName + ". Please try again!", ex); } }
public IReadOnlyDictionary <string, object> ToDictionary() { var d = new Dictionary <string, object> { ["name"] = $"{Name}", ["description"] = Description, ["tags"] = Tags }; string contentType = "image/png"; if (File is FileStream fileStream) { contentType = $"image/{Path.GetExtension(fileStream.Name)}"; } else if (FileName != null) { contentType = $"image/{Path.GetExtension(FileName)}"; } d["file"] = new MultipartFile(File, FileName ?? "image", contentType.Replace(".", "")); return(d); }
public Blob(MultipartFile fResult) { Type = fResult.Type; Size = fResult.Size; Name = fResult.Name; }
public IReadOnlyDictionary <string, object> ToDictionary() { var d = new Dictionary <string, object>(); var payload = new Dictionary <string, object>(); if (Content.IsSpecified) { payload["content"] = Content.Value; } if (IsTTS.IsSpecified) { payload["tts"] = IsTTS.Value.ToString(); } if (Nonce.IsSpecified) { payload["nonce"] = Nonce.Value; } if (Username.IsSpecified) { payload["username"] = Username.Value; } if (AvatarUrl.IsSpecified) { payload["avatar_url"] = AvatarUrl.Value; } if (MessageComponents.IsSpecified) { payload["components"] = MessageComponents.Value; } if (Embeds.IsSpecified) { payload["embeds"] = Embeds.Value; } if (AllowedMentions.IsSpecified) { payload["allowed_mentions"] = AllowedMentions.Value; } if (Flags.IsSpecified) { payload["flags"] = Flags.Value; } List <object> attachments = new(); for (int n = 0; n != Files.Length; n++) { var attachment = Files[n]; var filename = attachment.FileName ?? "unknown.dat"; if (attachment.IsSpoiler && !filename.StartsWith(AttachmentExtensions.SpoilerPrefix)) { filename = filename.Insert(0, AttachmentExtensions.SpoilerPrefix); } d[$"files[{n}]"] = new MultipartFile(attachment.Stream, filename); attachments.Add(new { id = (ulong)n, filename = filename, description = attachment.Description ?? Optional <string> .Unspecified }); } payload["attachments"] = attachments; var json = new StringBuilder(); using (var text = new StringWriter(json)) using (var writer = new JsonTextWriter(text)) _serializer.Serialize(writer, payload); d["payload_json"] = json.ToString(); return(d); }
public IReadOnlyDictionary <string, object> ToDictionary() { var d = new Dictionary <string, object>(); var payload = new Dictionary <string, object>(); var message = new Dictionary <string, object>(); payload["name"] = Title; payload["auto_archive_duration"] = ArchiveDuration; if (Slowmode.IsSpecified) { payload["rate_limit_per_user"] = Slowmode.Value; } // message if (Content.IsSpecified) { message["content"] = Content.Value; } if (Embeds.IsSpecified) { message["embeds"] = Embeds.Value; } if (AllowedMentions.IsSpecified) { message["allowed_mentions"] = AllowedMentions.Value; } if (MessageComponent.IsSpecified) { message["components"] = MessageComponent.Value; } if (Stickers.IsSpecified) { message["sticker_ids"] = Stickers.Value; } if (Flags.IsSpecified) { message["flags"] = Flags.Value; } List <object> attachments = new(); for (int n = 0; n != Files.Length; n++) { var attachment = Files[n]; var filename = attachment.FileName ?? "unknown.dat"; if (attachment.IsSpoiler && !filename.StartsWith(AttachmentExtensions.SpoilerPrefix)) { filename = filename.Insert(0, AttachmentExtensions.SpoilerPrefix); } d[$"files[{n}]"] = new MultipartFile(attachment.Stream, filename); attachments.Add(new { id = (ulong)n, filename = filename, description = attachment.Description ?? Optional <string> .Unspecified }); } message["attachments"] = attachments; payload["message"] = message; var json = new StringBuilder(); using (var text = new StringWriter(json)) using (var writer = new JsonTextWriter(text)) _serializer.Serialize(writer, payload); d["payload_json"] = json.ToString(); return(d); }
public static bool GetFiles(this Stream stream, MultipartModel model) { var result = false; var data = ToByteArray(stream); var content = data.GetString(); var delimiterEndIndex = content.IndexOf("\r\n"); if (delimiterEndIndex > -1) { var delimiter = content.Substring(0, delimiterEndIndex); var delimiterBytes = ("\r\n" + delimiter).GetBytes(); var regexFileName = new Regex(@"(?<=filename\=\"")(.*?)(?=\"")"); var regexContentType = new Regex(@"(?<=Content\-Type:)(.*?)(?=\r\n\r\n)"); model.files = new List <MultipartFile>(); for (var endIndex = 0; endIndex < data.Length;) { try { var filenameMatch = regexFileName.Match(content, endIndex); var contentTypeMatch = regexContentType.Match(content, endIndex); if (contentTypeMatch.Success && filenameMatch.Success) { var startIndex = contentTypeMatch.Index + contentTypeMatch.Length + "\r\n\r\n".Length; endIndex = IndexOf(data, delimiterBytes, startIndex); var contentLength = endIndex - startIndex; if (contentLength > 0) { var fileData = new byte[contentLength]; var file = new MultipartFile(); file.ContentType = contentTypeMatch.Value.Trim(); file.Filename = filenameMatch.Value.Trim(); Buffer.BlockCopy(data, startIndex, fileData, 0, contentLength); file.FileContents = fileData; model.files.Add(file); } else { break; } } else { break; } } catch (Exception ex) { ex.DebugLog(); break; } } result = true; } return(result); }