Added download count logic
- Removed some comments
This commit is contained in:
parent
0d517dc8df
commit
e3f14801e9
@ -36,14 +36,11 @@ public class FileUploadRepository {
|
||||
}
|
||||
|
||||
public void insertFileUpload(FileUpload fileUpload) {
|
||||
// Serialize FileUpload object to JSON
|
||||
String json = gson.toJson(fileUpload);
|
||||
|
||||
// Convert JSON string back to Map
|
||||
Type type = new TypeToken<Map<String, Object>>(){}.getType();
|
||||
Map<String, Object> map = gson.fromJson(json, type);
|
||||
|
||||
// Insert the Map into RethinkDB
|
||||
r.db("sharepulse").table("file_uploads").insert(map).run(connection);
|
||||
}
|
||||
|
||||
@ -53,4 +50,18 @@ public class FileUploadRepository {
|
||||
.run(connection, FileUpload.class)
|
||||
.next();
|
||||
}
|
||||
|
||||
public void updateFileUpload(FileUpload updatedFileUpload) {
|
||||
String json = gson.toJson(updatedFileUpload);
|
||||
|
||||
Type type = new TypeToken<Map<String, Object>>(){}.getType();
|
||||
Map<String, Object> map = gson.fromJson(json, type);
|
||||
|
||||
String fileId = updatedFileUpload.getFileId();
|
||||
|
||||
r.db("sharepulse").table("file_uploads")
|
||||
.filter(r.hashMap("fileId", fileId))
|
||||
.update(map)
|
||||
.run(connection);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package de.w665.sharepulse.rest.mappings;
|
||||
|
||||
import de.w665.sharepulse.exception.NoDownloadPermissionException;
|
||||
import de.w665.sharepulse.model.FileUpload;
|
||||
import de.w665.sharepulse.rest.ApiRestController;
|
||||
import de.w665.sharepulse.service.FileSecurityService;
|
||||
@ -9,8 +10,10 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.ErrorResponse;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
@ -18,6 +21,8 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@ -33,28 +38,33 @@ public class Download extends ApiRestController {
|
||||
}
|
||||
|
||||
@GetMapping("/download")
|
||||
public ResponseEntity<Resource> download(@RequestParam String fileId,
|
||||
@RequestParam(value = "password", required = false) String password) throws IOException {
|
||||
public ResponseEntity<Object> download(@RequestParam String fileId,
|
||||
@RequestParam(value = "password", required = false) String password) throws IOException {
|
||||
|
||||
|
||||
FileUpload fileUpload = fileService.getFileUploadByFileId(fileId);
|
||||
|
||||
fileSecurityService.verifyDownloadPermission(fileUpload, password);
|
||||
try {
|
||||
fileSecurityService.verifyDownloadPermission(fileUpload, password);
|
||||
} catch (NoDownloadPermissionException e) {
|
||||
log.debug("No download permission for file: " + fileId);
|
||||
ErrorResponse errorResponse = ErrorResponse.builder(e, HttpStatus.FORBIDDEN, e.getMessage())
|
||||
.build();
|
||||
return new ResponseEntity<>(errorResponse, HttpStatus.FORBIDDEN);
|
||||
}
|
||||
|
||||
// TODO: Update download count in database (single download will not work without this)
|
||||
fileService.updateDownloadCount(fileUpload);
|
||||
|
||||
File file = fileService.getFileById(fileId);
|
||||
InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
|
||||
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"edmbass.flac\"");
|
||||
//headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getName() + "\"");
|
||||
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileUpload.getFileName() + "\"");
|
||||
headers.add(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, must-revalidate");
|
||||
headers.add(HttpHeaders.PRAGMA, "no-cache");
|
||||
headers.add(HttpHeaders.EXPIRES, "0");
|
||||
|
||||
InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.headers(headers)
|
||||
.contentLength(file.length())
|
||||
|
@ -95,6 +95,12 @@ public class FileService {
|
||||
return fileUploadRepository.retrieveFileUploadByFileId(fileId);
|
||||
}
|
||||
|
||||
public long updateDownloadCount(FileUpload fileUpload) {
|
||||
fileUpload.setDownloadCount(fileUpload.getDownloadCount() + 1);
|
||||
fileUploadRepository.updateFileUpload(fileUpload);
|
||||
return fileUpload.getDownloadCount();
|
||||
}
|
||||
|
||||
|
||||
private String getTempDirPath() {
|
||||
return System.getProperty("user.dir") + tempDirPath;
|
||||
|
Loading…
x
Reference in New Issue
Block a user