Update ScreenshotPlugin.java

This commit is contained in:
Kyle
2019-08-27 15:08:50 +01:00
committed by GitHub
parent c8296bf2ed
commit 3ac0bf2feb

View File

@@ -32,7 +32,10 @@ import com.google.inject.Provides;
import java.awt.Desktop; import java.awt.Desktop;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Image; import java.awt.Image;
import java.awt.Toolkit;
import java.awt.TrayIcon; import java.awt.TrayIcon;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@@ -98,7 +101,6 @@ import net.runelite.client.ui.ClientUI;
import net.runelite.client.ui.DrawManager; import net.runelite.client.ui.DrawManager;
import net.runelite.client.ui.NavigationButton; import net.runelite.client.ui.NavigationButton;
import net.runelite.client.ui.overlay.OverlayManager; import net.runelite.client.ui.overlay.OverlayManager;
import net.runelite.client.util.Clipboard;
import net.runelite.client.util.HotkeyListener; import net.runelite.client.util.HotkeyListener;
import net.runelite.client.util.ImageUtil; import net.runelite.client.util.ImageUtil;
import net.runelite.api.util.Text; import net.runelite.api.util.Text;
@@ -781,6 +783,13 @@ public class ScreenshotPlugin extends Plugin
} }
} }
/**
* Uploads a screenshot to the Imgur image-hosting service,
* and copies the image link to the clipboard.
*
* @param screenshotFile Image file to upload.
* @throws IOException Thrown if the file cannot be read.
*/
/** /**
* Uploads a screenshot to the Imgur image-hosting service, * Uploads a screenshot to the Imgur image-hosting service,
* and copies the image link to the clipboard. * and copies the image link to the clipboard.
@@ -792,50 +801,44 @@ public class ScreenshotPlugin extends Plugin
{ {
String json = RuneLiteAPI.GSON.toJson(new ImageUploadRequest(screenshotFile)); String json = RuneLiteAPI.GSON.toJson(new ImageUploadRequest(screenshotFile));
Request request = null; Request request = new Request.Builder()
if (IMGUR_IMAGE_UPLOAD_URL != null) .url(IMGUR_IMAGE_UPLOAD_URL)
{ .addHeader("Authorization", "Client-ID " + IMGUR_CLIENT_ID)
RequestBody body = RequestBody.Companion.create(json, JSON); .post(RequestBody.create(JSON, json))
request = new Request.Builder() .build();
.url(IMGUR_IMAGE_UPLOAD_URL)
.addHeader("Authorization", "Client-ID " + IMGUR_CLIENT_ID)
.post(body)
.build();
}
if (request != null) RuneLiteAPI.CLIENT.newCall(request).enqueue(new Callback()
{ {
RuneLiteAPI.CLIENT.newCall(request).enqueue(new Callback() @Override
public void onFailure(Call call, IOException ex)
{ {
@Override log.warn("error uploading screenshot", ex);
public void onFailure(Call call, IOException ex) }
{
log.warn("error uploading screenshot", ex);
}
@Override @Override
public void onResponse(Call call, Response response) throws IOException public void onResponse(Call call, Response response) throws IOException
{
try (InputStream in = response.body().byteStream())
{ {
try (InputStream in = response.body().byteStream()) ImageUploadResponse imageUploadResponse = RuneLiteAPI.GSON
.fromJson(new InputStreamReader(in), ImageUploadResponse.class);
if (imageUploadResponse.isSuccess())
{ {
ImageUploadResponse imageUploadResponse = RuneLiteAPI.GSON String link = imageUploadResponse.getData().getLink();
.fromJson(new InputStreamReader(in), ImageUploadResponse.class);
if (imageUploadResponse.isSuccess()) StringSelection selection = new StringSelection(link);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(selection, selection);
if (notifyWhenTaken)
{ {
String link = imageUploadResponse.getData().getLink(); notifier.notify("A screenshot was uploaded and inserted into your clipboard!", TrayIcon.MessageType.INFO);
Clipboard.store(link);
if (notifyWhenTaken)
{
notifier.notify("A screenshot was uploaded and inserted into your clipboard!", TrayIcon.MessageType.INFO);
}
} }
} }
} }
}); }
} });
} }
@VisibleForTesting @VisibleForTesting