fixed #525: Added MediaCrush custom API URL support (Example: https://imgrush.com)

This commit is contained in:
Jaex 2015-02-25 22:00:34 +02:00
parent 32ace62a2c
commit 8797bb7fc4
3 changed files with 51 additions and 61 deletions

View file

@ -1,4 +1,5 @@
<root>
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
@ -585,7 +586,7 @@ Voulez-vous la télécharger et l'installer?</value>
<value>Mettre en ligne depuis le presse-papier avec le visualiseur de contenu</value>
</data>
<data name="HotkeyType_UploadURL" xml:space="preserve">
<value>Mise en ligne du depuis l'URL...</value>
<value>Mise en ligne depuis l'URL...</value>
</data>
<data name="AfterCaptureTasks_UploadImageToHost" xml:space="preserve">
<value>Mettre en ligne l'image vers l'hôte</value>

View file

@ -27,6 +27,7 @@ You should have received a copy of the GNU General Public License
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ShareX.HelpersLib;
using System;
using System.IO;
using System.Net;
@ -37,8 +38,19 @@ namespace ShareX.UploadersLib.FileUploaders
{
public class MediaCrushUploader : FileUploader
{
public string APIURL { get; private set; }
public bool DirectLink { get; set; }
public MediaCrushUploader()
{
APIURL = "https://mediacru.sh";
}
public MediaCrushUploader(string apiURL)
{
APIURL = URLHelpers.FixPrefix(apiURL);
}
public override UploadResult Upload(Stream stream, string fileName)
{
ThrowWebExceptions = true;
@ -54,7 +66,7 @@ public override UploadResult Upload(Stream stream, string fileName)
try
{
result = UploadData(stream, "https://mediacru.sh/api/upload/file", fileName);
result = UploadData(stream, URLHelpers.CombineURL(APIURL, "api/upload/file"), fileName);
}
catch (WebException e)
{
@ -77,7 +89,7 @@ public override UploadResult Upload(Stream stream, string fileName)
while (!StopUploadRequested)
{
result.Response = SendRequest(HttpMethod.GET, "https://mediacru.sh/api/" + hash + "/status");
result.Response = SendRequest(HttpMethod.GET, URLHelpers.CombineURL(APIURL, "api/" + hash + "/status"));
JToken jsonResponse = JToken.Parse(result.Response);
string status = jsonResponse["status"].Value<string>();
@ -90,9 +102,7 @@ public override UploadResult Upload(Stream stream, string fileName)
case "done":
case "ready":
MediaCrushBlob blob = jsonResponse[hash].ToObject<MediaCrushBlob>();
result.URL = DirectLink ? blob.DirectURL : blob.URL;
result.DeletionURL = blob.DeletionURL;
return result;
return UpdateResult(result, blob);
case "unrecognized":
// Note: MediaCrush accepts just about _every_ kind of media file,
// so the file itself is probably corrupted or just not actually a media file
@ -129,28 +139,20 @@ private UploadResult HandleDuplicate(HttpWebResponse httpResponse)
string hash = response["hash"].Value<string>();
MediaCrushBlob blob = response[hash].ToObject<MediaCrushBlob>();
return new UploadResult
{
URL = DirectLink ? blob.DirectURL : blob.URL,
DeletionURL = blob.DeletionURL
};
return UpdateResult(new UploadResult(), blob);
}
private UploadResult CheckExists(string hash)
{
try
{
string response = SendRequest(HttpMethod.GET, "https://mediacru.sh/api/" + hash);
string response = SendRequest(HttpMethod.GET, URLHelpers.CombineURL(APIURL, "api/" + hash));
if (!string.IsNullOrEmpty(response))
{
MediaCrushBlob blob = JsonConvert.DeserializeObject<MediaCrushBlob>(response);
return new UploadResult(response)
{
URL = DirectLink ? blob.DirectURL : blob.URL,
DeletionURL = blob.DeletionURL
};
return UpdateResult(new UploadResult(response), blob);
}
}
catch
@ -159,6 +161,31 @@ private UploadResult CheckExists(string hash)
return null;
}
private UploadResult UpdateResult(UploadResult result, MediaCrushBlob blob)
{
string url = URLHelpers.CombineURL(APIURL, blob.Hash);
if (DirectLink)
{
if (blob.Files != null && blob.Files.Length > 0)
{
if (blob.BlobType == "image")
{
url = blob.Files[0].URL;
}
else if (blob.BlobType == "video" || blob.BlobType == "audio")
{
url = URLHelpers.CombineURL(APIURL, blob.Hash + "/direct");
}
}
}
result.URL = url;
result.DeletionURL = URLHelpers.CombineURL(APIURL, blob.Hash + "/delete");
return result;
}
}
internal class MediaCrushBlob
@ -187,44 +214,5 @@ public class MediaCrushFile
public string UserMimetype { get; set; }
[JsonProperty("hash")]
public string Hash { get; set; }
[JsonIgnore]
public string URL
{
get
{
return "https://mediacru.sh/" + Hash;
}
}
[JsonIgnore]
public string DirectURL
{
get
{
if (Files != null && Files.Length > 0)
{
if (BlobType == "image")
{
return Files[0].URL;
}
else if (BlobType == "video" || BlobType == "audio")
{
return "https://mediacru.sh/" + Hash + "/direct";
}
}
return URL;
}
}
[JsonIgnore]
public string DeletionURL
{
get
{
return "https://mediacru.sh/" + Hash + "/delete";
}
}
}
}

View file

@ -1,3 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
@ -256,13 +257,13 @@
<value>294, 22</value>
</data>
<data name="tsmiUploadClipboard.Text" xml:space="preserve">
<value>Mise en ligne du depuis le presse-papier...</value>
<value>Mise en ligne depuis le presse-papier...</value>
</data>
<data name="tsmiUploadURL.Size" type="System.Drawing.Size, System.Drawing">
<value>294, 22</value>
</data>
<data name="tsmiUploadURL.Text" xml:space="preserve">
<value>Mise en ligne du depuis l'URL...</value>
<value>Mise en ligne depuis l'URL...</value>
</data>
<data name="tsmiUploadDragDrop.Size" type="System.Drawing.Size, System.Drawing">
<value>294, 22</value>
@ -889,13 +890,13 @@
<value>294, 22</value>
</data>
<data name="tsmiTrayUploadClipboard.Text" xml:space="preserve">
<value>Mise en ligne du depuis le presse-papier...</value>
<value>Mise en ligne depuis le presse-papier...</value>
</data>
<data name="tsmiTrayUploadURL.Size" type="System.Drawing.Size, System.Drawing">
<value>294, 22</value>
</data>
<data name="tsmiTrayUploadURL.Text" xml:space="preserve">
<value>Mise en ligne du depuis l'URL...</value>
<value>Mise en ligne depuis l'URL...</value>
</data>
<data name="tsmiTrayUploadDragDrop.Size" type="System.Drawing.Size, System.Drawing">
<value>294, 22</value>