Skip to content

Commit

Permalink
File updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisMitt committed Oct 17, 2023
1 parent 1432be5 commit 8418074
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
Expand All @@ -18,9 +19,9 @@ public class StorageController {

private StorageService service = new StorageService();

@PostMapping("/upload")
public ResponseEntity<String> uploadFile(@RequestParam(value = "file") MultipartFile file){
return new ResponseEntity<>(service.uploadFile(file), HttpStatus.OK);
@PostMapping("/upload/{dsid}")
public ResponseEntity<String> uploadFile(@RequestParam(value = "file") MultipartFile file, @PathVariable String dsid) throws IOException {
return new ResponseEntity<>(service.uploadFile(file, dsid), HttpStatus.OK);
}

@GetMapping("/download/{fileName}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import software.amazon.awssdk.utils.IoUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
Expand Down Expand Up @@ -46,12 +43,16 @@ public AmazonS3 initializeS3Client(){



public String uploadFile(MultipartFile file){
public String uploadFile(MultipartFile file, String dsid) throws IOException {

Date date = new Date();

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String fileDate = formatter.format(date);
File fileObj = convertMultiPartFiletoFile(file);
String fileName = fileDate + "-DatascopeID=" + file.getOriginalFilename();
int id = Integer.parseInt(dsid);

String fileName = fileDate + "-DatascopeID=" + id + "-" + file.getOriginalFilename();
try {
this.s3Client.putObject(new PutObjectRequest(bucketName, fileName, fileObj));
}
Expand Down
27 changes: 22 additions & 5 deletions frontend/infosafe_frontend/src/components/FilePopup.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {useGetFiles} from "./getData/getFiles";

export const FilePopup = ({ popupOpen, popupClose, datascope }) => {
const [selectedFile, setSelectedFile] = useState(null);
const {showFile, fetchAllFiles} = useGetFiles();
const {showFile, loading, fetchAllFiles} = useGetFiles();
const FILES = [];

useEffect(() => {
Expand All @@ -20,7 +20,24 @@ import {useGetFiles} from "./getData/getFiles";
showFile.map((data) =>
FILES.push(data)
);
if (FILES.length === 0)

for (let i=0; i<FILES.length; i++){
let fileID = FILES[i].substring(23);
fileID = fileID.slice(0, 2);

if(fileID[fileID.length-1] === "-"){
fileID = fileID.substring(0, fileID.length-1);
}
const int = parseInt(fileID);
if(int !== datascope.data_scope_id){

}
}

if (loading === true){
FILES[0] = "Files Loading.....";
}
else if (FILES.length === 0)
{
FILES[0] = "No Files added yet.";
}
Expand All @@ -36,9 +53,10 @@ import {useGetFiles} from "./getData/getFiles";
}

const formData = new FormData();
selectedFile.title = "help";
formData.append("file", selectedFile);

fetch("http://localhost:8080/api/storage/upload", {
fetch(`http://localhost:8080/api/storage/upload/${datascope.data_scope_id}`, {
method: "POST",
headers: {
Authorization: "Bearer " + sessionStorage.getItem('accessToken')
Expand All @@ -48,9 +66,8 @@ import {useGetFiles} from "./getData/getFiles";
.then((response) => {
if (response.ok) {
// Handle the response as needed
//console.log(response);
return response.text();
popupClose();
return response.text();
} else {
//console.log(response);
throw new Error("File upload failed");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {CreateDataScopePopup} from "../Create/CreateDataScopePopup";
import React, {useEffect, useState} from "react";
import ViewDataScope from "../View/ViewDataScope";
import {FaRegEdit, FaSearch} from "react-icons/fa";
import {FaRegEdit, FaSearch, FaFileAlt} from "react-icons/fa";
import {EditDataScopePopup} from "../Edit/EditDataScopePopup";
import {RiDeleteBin6Fill, RiEditBoxFill} from "react-icons/ri";
import "../../styling/DataScopes.css";
Expand Down Expand Up @@ -111,7 +111,7 @@ export const DataScopes = () => {
<EditDataScope datascope={datascope}></EditDataScope>
<DeleteDataScope datascope={datascope}></DeleteDataScope>
<div className="viewFiles">
<button className="filesButton" onClick={() => setViewFilesOpen(true)}>Files</button>
<FaFileAlt className="filesButton" onClick={() => setViewFilesOpen(true)}></FaFileAlt>
{viewFilesOpen ? (
<FilePopup datascope={datascope}
popupClose={() => setViewFilesOpen(false)}
Expand Down

0 comments on commit 8418074

Please sign in to comment.