-
Notifications
You must be signed in to change notification settings - Fork 122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a Java function #178
Open
ShizhanLiu
wants to merge
1
commit into
open-runtimes:main
Choose a base branch
from
ShizhanLiu:ShizhanLiu-feat-4114-generateWebsiteScreenshot-java
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add a Java function #178
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import com.google.gson.Gson; | ||
import java.io.IOException; | ||
import java.math.BigDecimal; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import org.apache.tomcat.util.codec.binary.Base64; | ||
import sun.misc.BASE64Encoder; | ||
import javax.imageio.ImageIO; | ||
import java.awt.*; | ||
import java.awt.event.KeyEvent; | ||
import java.awt.image.BufferedImage; | ||
import java.io.*; | ||
import java.net.*; | ||
import java.nio.file.Files; | ||
import java.util.UUID; | ||
|
||
/** | ||
* @Author:Shizhan | ||
* @ProjectName:screenshot | ||
* @currentTime: 2023/7/26 16:02 | ||
*/ | ||
|
||
public RuntimeResponse main(RuntimeRequest req, RuntimeResponse res) throws AWTException, URISyntaxException, IOException { | ||
String payloadString = req.getPayload(); | ||
final Gson gson = new Gson(); | ||
Map<String, Object> payload = gson.fromJson(payloadString, Map.class); | ||
Map<String, Object> responseData = new HashMap<>(); | ||
//get the url | ||
String urlInput = payload.get("url").toString(); | ||
//test if the url is valid | ||
long lo = System.currentTimeMillis(); | ||
boolean isValid = false; | ||
URL url; | ||
try { | ||
url = new URL(urlInput); | ||
InputStream in = url.openStream(); | ||
//the input is true | ||
isValid = true; | ||
} catch (Exception e1) { | ||
//the input is false | ||
url = null; | ||
} | ||
//if the url is not valid, return false | ||
if(!isValid){ | ||
responseData.put("success",false); | ||
responseData.put("message","Website could not be reached."); | ||
return res.json(responseData); | ||
} | ||
//get the absolute path | ||
File file = new File("src/main/resources/upload"); | ||
String pathname = file.getAbsolutePath(); | ||
//JDK 1.6 and up | ||
Desktop.getDesktop().browse(new URI(urlInput)); | ||
Robot robot = new Robot(); | ||
robot.delay(5000); | ||
Dimension d = new Dimension(Toolkit.getDefaultToolkit().getScreenSize()); | ||
int width = (int) d.getWidth(); | ||
int height = (int) d.getHeight(); | ||
//maximize the browser. | ||
robot.keyPress(KeyEvent.VK_F11); | ||
robot.delay(2000); | ||
Image image = robot.createScreenCapture(new Rectangle(0, 0, width, height)); | ||
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); | ||
Graphics q = bi.createGraphics(); | ||
q.drawImage(image, 0, 0, width, height, null); | ||
//get the File | ||
File outputfile = new File(pathname + "/"+ UUID.randomUUID() +".jpg"); | ||
//get the absolute path | ||
String absolutePath = outputfile.getAbsolutePath(); | ||
ImageIO.write(bi, "jpg", outputfile); | ||
|
||
// transfer to base 64 format. | ||
byte[] fileByte = null; | ||
String base64Str = ""; | ||
try { | ||
File fileNew = new File(absolutePath); | ||
fileByte = Files.readAllBytes(fileNew.toPath()); | ||
base64Str = "data:image/png;base64," + Base64.encodeBase64String(fileByte); | ||
} catch (IOException e) { | ||
//return the false message | ||
e.printStackTrace(); | ||
responseData.put("success",false); | ||
responseData.put("message","Website could not be reached."); | ||
return res.json(responseData); | ||
} | ||
//return the corrent value. | ||
responseData.put("success", true); | ||
responseData.put("screenshot", base64Str); | ||
return res.json(responseData); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# 💻 Get Short URL | ||
|
||
A Java Cloud Function to get price of crypto. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line should be rectified to mention that this function is used to generate srceenshots, also mention the file types supported |
||
|
||
```json | ||
{ | ||
"payload": { | ||
"url": "https://google.com/" | ||
} | ||
} | ||
``` | ||
|
||
_Example output:_ | ||
|
||
|
||
```json | ||
{ | ||
"success":true, | ||
"screenshot":"iVBORw0KGgoAAAANSUhEUgAAAaQAAALiCAY...QoH9hbkTPQAAAABJRU5ErkJggg==" | ||
} | ||
``` | ||
|
||
_Error Example output:_ | ||
|
||
```json | ||
{ | ||
"success": false, | ||
"message":"Website could not be reached." | ||
} | ||
``` | ||
|
||
|
||
## 📝 Environment Variables | ||
|
||
List of environment variables used by this cloud function. | ||
|
||
## 🚀 Deployment | ||
|
||
1. Clone this repository, and enter this function folder: | ||
|
||
``` | ||
$ git clone https://github.com/open-runtimes/examples.git && cd examples | ||
$ cd java/generate_screenshot | ||
``` | ||
|
||
2. Enter this function folder and build the code: | ||
``` | ||
docker run -e INTERNAL_RUNTIME_ENTRYPOINT=Index.java --rm --interactive --tty --volume $PWD:/usr/code openruntimes/java:v2-18.0 sh /usr/local/src/build.sh | ||
``` | ||
As a result, a `code.tar.gz` file will be generated. | ||
|
||
3. Start the Open Runtime: | ||
``` | ||
docker run -p 3000:3000 -e INTERNAL_RUNTIME_KEY=secret-key --rm --interactive --tty --volume $PWD/code.tar.gz:/tmp/code.tar.gz:ro openruntimes/java:v2-18.0 sh /usr/local/src/start.sh | ||
``` | ||
|
||
Your function is now listening on port `3000`, and you can execute it by sending `POST` request with appropriate authorization headers. To learn more about runtime, you can visit Java runtime [README](https://github.com/open-runtimes/open-runtimes/tree/main/runtimes/java-18.0). | ||
|
||
## 📝 Notes | ||
- This function is designed for use with Appwrite Cloud Functions. You can learn more about it in [Appwrite docs](https://appwrite.io/docs/functions). | ||
- This example is compatible with Java 18.0. Other versions may work but are not guarenteed to work as they haven't been tested. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
dependencies { | ||
implementation 'com.google.code.gson:gson:2.9.0' | ||
implementation 'org.apache.tomcat:tomcat-util:8.5.23' | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be
# Get Screenshot