1- // https://gist.github.com/b5e14d0c36f5a972060655b1aa875dbf
2- // CODE HERE COMES FROM ABOVE GIST. MAY BE MODIFIED.
3- // All rights belong to original creator, and not the author of this software.
1+ // Based on https://gist.github.com/b5e14d0c36f5a972060655b1aa875dbf
42
53namespace Cliptok . Helpers
64{
75 public class HasteBinClient
86 {
97 private static readonly HttpClient _httpClient ;
108 private readonly string _baseUrl ;
9+ public readonly string _hasteType = "haste" ;
1110
1211 static HasteBinClient ( )
1312 {
1413 _httpClient = new HttpClient ( ) ;
1514 }
1615
17- public HasteBinClient ( string baseUrl )
16+ public HasteBinClient ( string baseUrl , string hasteType = "haste" )
1817 {
1918 _baseUrl = baseUrl ;
19+ _hasteType = hasteType ;
2020 }
2121
22- public async Task < HasteBinResult > Post ( string content )
22+ public async Task < HasteBinResult > PostAsync ( string content , string language = default )
23+ {
24+ switch ( _hasteType )
25+ {
26+ case "haste" :
27+ return await PostHastebinAsync ( content , language ) ;
28+ case "tclip" :
29+ return await PostTclipAsync ( content , language ) ;
30+ default :
31+ throw new NotSupportedException ( $ "Haste type '{ _hasteType } ' is not supported.") ;
32+ }
33+ }
34+
35+ public async Task < HasteBinResult > PostHastebinAsync ( string content , string language )
2336 {
2437 string fullUrl = _baseUrl ;
2538 if ( ! fullUrl . EndsWith ( "/" ) )
@@ -44,6 +57,12 @@ public async Task<HasteBinResult> Post(string content)
4457 hasteBinResult . FullUrl = $ "{ fullUrl } { hasteBinResult . Key } ";
4558 hasteBinResult . IsSuccess = true ;
4659 hasteBinResult . StatusCode = 200 ;
60+ hasteBinResult . RawUrl = $ "{ fullUrl } raw/{ hasteBinResult . Key } ";
61+
62+ if ( language != default )
63+ {
64+ hasteBinResult . FullUrl = $ "{ hasteBinResult . FullUrl } .{ language } ";
65+ }
4766 return hasteBinResult ;
4867 }
4968 }
@@ -55,13 +74,69 @@ public async Task<HasteBinResult> Post(string content)
5574 StatusCode = ( int ) result . StatusCode
5675 } ;
5776 }
77+
78+ public async Task < HasteBinResult > PostTclipAsync ( string content , string language )
79+ {
80+ string fullUrl = _baseUrl ;
81+ if ( ! fullUrl . EndsWith ( "/" ) )
82+ {
83+ fullUrl += "/" ;
84+ }
85+ string postUrl = $ "{ fullUrl } api/post";
86+ if ( language == default )
87+ language = "txt" ;
88+
89+ var formdata = new MultipartFormDataContent
90+ {
91+ { new StringContent ( content ) , "content" } ,
92+ { new StringContent ( Program . discord . CurrentUser . Username + "." + ( language ) ) , "filename" }
93+ } ;
94+ //formdata.Add(new StringContent(Program.discord.CurrentUser.Username) + "." + language), "filename")
95+
96+ HttpRequestMessage request = new HttpRequestMessage ( HttpMethod . Post , new Uri ( postUrl ) )
97+ {
98+ Content = formdata ,
99+ Headers = { { "Accept" , "text/plain" } }
100+ } ;
101+
102+ HttpResponseMessage result = await _httpClient . SendAsync ( request ) ;
103+
104+ if ( result . StatusCode == HttpStatusCode . OK )
105+ {
106+ var responseText = await result . Content . ReadAsStringAsync ( ) ;
107+ HasteBinResult hasteBinResult = new HasteBinResult
108+ {
109+ FullUrl = responseText ,
110+ RawUrl = responseText + "/raw" ,
111+ IsSuccess = true ,
112+ StatusCode = 200
113+ } ;
114+ return hasteBinResult ;
115+ }
116+
117+ return new HasteBinResult ( )
118+ {
119+ FullUrl = fullUrl ,
120+ IsSuccess = false ,
121+ StatusCode = ( int ) result . StatusCode
122+ } ;
123+ }
124+
125+ }
126+
127+ public class TclipRequest
128+ {
129+ [ JsonProperty ( "content" ) ]
130+ public string Content { get ; set ; }
131+ [ JsonProperty ( "filename" ) ]
132+ public string Filename { get ; set ; }
58133 }
59134
60- // Define other methods and classes here
61135 public class HasteBinResult
62136 {
63- public string Key { get ; set ; }
137+ public string Key { get ; set ; } = null ;
64138 public string FullUrl { get ; set ; }
139+ public string RawUrl { get ; set ; }
65140 public bool IsSuccess { get ; set ; }
66141 public int StatusCode { get ; set ; }
67142 }
0 commit comments