Skip to content

Commit

Permalink
add apk to server
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunhThanhDe committed Apr 21, 2023
1 parent 1c3f152 commit 7a62a32
Show file tree
Hide file tree
Showing 10 changed files with 308 additions and 17 deletions.
15 changes: 15 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@
<artifactId>modelmapper</artifactId>
<version>2.3.8</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
<dependency>
<groupId>net.dongliu</groupId>
<artifactId>apk-parser</artifactId>
<version>2.6.9</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

<build>
Expand Down
69 changes: 69 additions & 0 deletions src/main/java/com/vnptt/tms/api/ApkApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@
import com.vnptt.tms.dto.ApkDTO;
import com.vnptt.tms.service.IApkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@CrossOrigin
@RestController
Expand All @@ -18,6 +25,13 @@ public class ApkApi {
@Autowired
private IApkService apkService;

/**
* get all apk infor from server
*
* @param page
* @param limit
* @return
*/
@GetMapping(value = "/apk")
public ApkOutput showApk(@RequestParam(value = "page", required = false) Integer page,
@RequestParam(value = "limit", required = false) Integer limit) {
Expand All @@ -41,6 +55,37 @@ public ApkOutput showApk(@RequestParam(value = "page", required = false) Integer
return result;
}

/**
* dowload apk for box
*
* @param fileName
* @param request
* @return
*/
@GetMapping("/downloadFile/{fileName:.+}")
public ResponseEntity<Resource> downloadFile(@PathVariable String fileName, HttpServletRequest request) {

// Load file as Resource
Resource resource = apkService.loadFileAsResource(fileName);

// Try to determine file's content type
String contentType = null;
try {
contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
} catch (IOException ignored) {
}

// Fallback to the default content type if type could not be determined
if (contentType == null) {
contentType = "application/octet-stream";
}

return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(contentType))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
.body(resource);
}

/**
* find apk with id
*
Expand Down Expand Up @@ -85,11 +130,35 @@ public ApkDTO addApplicationToDevice(@PathVariable(value = "policyId") Long poli
return apkService.addApkToPolicy(policyId, apkId);
}

/**
* create apk normal
*
* @param model
* @return
*/
@PostMapping(value = "/apk")
public ApkDTO createApk(@RequestBody ApkDTO model) {
return apkService.save(model);
}

/**
* upload file to server and save apk info
*
* @param file
* @return
*/
@PostMapping("/uploadFile")
public ApkDTO uploadFile(@RequestParam("file") MultipartFile file) {
return apkService.saveFile(file);
}

/**
* update apk info
*
* @param model
* @param id
* @return
*/
@PutMapping(value = "/apk/{id}")
public ApkDTO updateApk(@RequestBody ApkDTO model, @PathVariable("id") Long id) {
model.setId(id);
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/vnptt/tms/config/FileStorageProperties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.vnptt.tms.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "file")
public class FileStorageProperties {
private String uploadDir;

public String getUploadDir() {
return uploadDir;
}

public void setUploadDir(String uploadDir) {
this.uploadDir = uploadDir;
}
}
7 changes: 4 additions & 3 deletions src/main/java/com/vnptt/tms/dto/ApkDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
public class ApkDTO extends AbstractDTO<ApkDTO>{
private String packagename;
private String apkfileUrl;
private int version;
private long version;
private String md5;
private Long packagesize;


public String getPackagename() {
return packagename;
}
Expand All @@ -23,11 +24,11 @@ public void setApkfileUrl(String apkfileUrl) {
this.apkfileUrl = apkfileUrl;
}

public int getVersion() {
public long getVersion() {
return version;
}

public void setVersion(int version) {
public void setVersion(long version) {
this.version = version;
}

Expand Down
14 changes: 10 additions & 4 deletions src/main/java/com/vnptt/tms/entity/ApkEntity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.vnptt.tms.entity;

import org.apache.commons.codec.digest.DigestUtils;

import javax.persistence.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -11,8 +16,8 @@ public class ApkEntity extends BaseEntity{
private String packagename;
@Column(name = "apkfileUrl", nullable = false)
private String apkfileUrl;
@Column(name = "version", nullable = false)
private int version;
@Column(name = "version")
private long version;
@Column(name ="md5", nullable = false)
private String md5;
@Column(name = "packagesize", nullable = false)
Expand Down Expand Up @@ -41,11 +46,11 @@ public void setApkfileUrl(String apkfileUrl) {
this.apkfileUrl = apkfileUrl;
}

public int getVersion() {
public long getVersion() {
return version;
}

public void setVersion(int version) {
public void setVersion(long version) {
this.version = version;
}

Expand All @@ -72,4 +77,5 @@ public List<PolicyEntity> getPolicyEntities() {
public void setPolicyEntities(List<PolicyEntity> policyEntities) {
this.policyEntities = policyEntities;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,26 @@ public ErrorMessage globalExceptionHandler(Exception ex, WebRequest request) {

return message;
}

@ExceptionHandler(FileStorageException.class)
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
public ErrorMessage FileStorageException(FileStorageException ex, WebRequest request) {
ErrorMessage message = new ErrorMessage(
HttpStatus.INTERNAL_SERVER_ERROR.value(),
new Date(),
ex.getMessage(),
request.getDescription(false));
return message;
}

@ExceptionHandler(FileNotFoundException.class)
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public ErrorMessage FileNotFoundException(FileNotFoundException ex, WebRequest request) {
ErrorMessage message = new ErrorMessage(
HttpStatus.INTERNAL_SERVER_ERROR.value(),
new Date(),
ex.getMessage(),
request.getDescription(false));
return message;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.vnptt.tms.exception;

/**
* Returns in case no result can be found in the database SQL
*/
public class ResourceNotFoundException extends RuntimeException {

private static final long serialVersionUID = 1L;
//private static final long serialVersionUID = 1L;

public ResourceNotFoundException(String msg) {
super(msg);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/vnptt/tms/service/IApkService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.vnptt.tms.service;

import com.vnptt.tms.dto.ApkDTO;
import org.springframework.core.io.Resource;
import org.springframework.data.domain.Pageable;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

Expand All @@ -23,4 +25,8 @@ public interface IApkService {
ApkDTO addApkToPolicy(Long policyId, Long apkId);

void removeApkinPolicy(Long policyId, Long apkId);

ApkDTO saveFile(MultipartFile file);

Resource loadFileAsResource(String fileName);
}
Loading

0 comments on commit 7a62a32

Please sign in to comment.