Skip to content

Commit

Permalink
add upload demo
Browse files Browse the repository at this point in the history
  • Loading branch information
v1ll4n committed Aug 7, 2024
1 parent c91590a commit 213f551
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/essential-for-syntaxflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:

- name: Download yak binary
run: |
wget -O yak https://aliyun-oss.yaklang.com/yak/1.3.5-beta2/yak_linux_amd64
wget -O yak https://aliyun-oss.yaklang.com/yak/1.3.5-beta3/yak_linux_amd64
chmod +x yak
- name: Setup PATH
Expand Down
15 changes: 15 additions & 0 deletions java-springboot-upload/java-springboot-upload.sf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
desc(
title: 'checking [Directly relative Files.write from MultipartFile]',
type: audit,
level: warning,
)

<include('spring-param')>?{<typeName>?{have: MultipartFile}} as $upload;
Files.write(*?{!opcode: param} as $sink);

$sink #{
until: `*?{<typeName>?{have: MultipartFile}} & $upload as $inter`
}->

check $inter then "Upload and Write Files directly" else "No Upload and Files.write"
alert $inter
41 changes: 41 additions & 0 deletions java-springboot-upload/sample/UploadDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.example.blog.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@RestController
@RequestMapping("/api/blog")
public class ImageUploadController {

private static final String UPLOAD_DIR = "/opt/blog/uploads/";

@PostMapping("/upload-image")
public String handleImageUpload(@RequestParam("image") MultipartFile file) {
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
String fileName = file.getOriginalFilename();
String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
if (!extension.equalsIgnoreCase("png") && !extension.equalsIgnoreCase("jpg") && !extension.equalsIgnoreCase("jpeg")) {
return "Only PNG, JPG and JPEG images are allowed.";
}
Path path = Paths.get(UPLOAD_DIR + fileName);
Files.write(path, bytes);
return "Image uploaded successfully: " + fileName;
} catch (IOException e) {
e.printStackTrace();
return "Failed to upload image: " + file.getOriginalFilename();
}
} else {
return "Failed to upload image because the file was empty.";
}
}
}
5 changes: 5 additions & 0 deletions java-struts-realworld/sample/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
./target/
target
target/
target/**
logs/**

0 comments on commit 213f551

Please sign in to comment.