Skip to content

Commit

Permalink
feat: dynamic storage stats
Browse files Browse the repository at this point in the history
  • Loading branch information
yatharthagoenka committed May 5, 2023
1 parent 4f613a1 commit a8b4911
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 32 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# BlueCloud
A cryptographic cloud-based file storage system.

Check it out [here](http://3.26.196.196/auth/login)


## Setting up docker for `server`:

Expand Down
2 changes: 2 additions & 0 deletions server/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ http {

listen 3000;

client_max_body_size 512M;

location / {
proxy_pass http://app_servers;
}
Expand Down
8 changes: 6 additions & 2 deletions server/src/files/files.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ export class FilesController {

@Delete('')
@UseGuards(AuthGuard("jwt"))
async deleteFile(@Query('fileID', new ValidateObjectId()) fileID, @Res() res,) {
async deleteFile(
@Query('fileID', new ValidateObjectId()) fileID,
@Query('userID', new ValidateObjectId()) userID,
@Res() res
) {
try {
await this.filesService.delete(fileID, 'all');
await this.filesService.delete(userID, fileID, 'all');
return res.status(HttpStatus.OK).json({ message: 'File deleted successfully' });
} catch (error) {
return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ error: error });
Expand Down
14 changes: 6 additions & 8 deletions server/src/files/files.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,6 @@ export class FilesService {
}

async encryptFile(savedFile) {
// const { stdout, stderr } = await execPromise(`python3 ${pythonScriptPath} enc ${savedFile.uuid}`) as child_process.ChildProcessWithoutNullStreams;
// if(stdout){
// const keys = JSON.parse(stdout.toString());
// this.loggerService.info(`encrypter.py: encrypted ${savedFile.uuid} : generated keys`);
// return keys;
// }
try {
const response = await axios.post('http://flask-service:5000/encrypt', {
uuid: savedFile.uuid,
Expand Down Expand Up @@ -104,19 +98,22 @@ export class FilesService {
// Saving to server
try{
savedFile = await this.saveFile(file);
var fileSize = (fs.statSync(path.join(__dirname, '..', '..', 'store', 'uploads', `${savedFile.uuid}`)).size)/1000;
var pub_key = await this.encryptFile(savedFile);
this.loggerService.info(`createFile: File ${savedFile.uuid} saved to server`);
}catch(error){
this.loggerService.error(`createFile: ${error}`);
return error;
}


// Saving to db
try{
const createFileDTO : IFile = {
originalname: `${savedFile.originalname}${savedFile.extension}`,
uuid: `${savedFile.uuid}`,
pub_key: `${pub_key}`,
size: fileSize,
ownerID: userID,
gems: [{
index: 0,
Expand All @@ -130,6 +127,7 @@ export class FilesService {
var userFileRecord : IUserFileRecord = {
originalname: savedFile.originalname,
fileID: resultCreateFile._id,
size: fileSize,
role: [IRole.OWNER, IRole.EDITOR, IRole.VIEWER]
}
await this.userService.addFileToUser(userID.toString(), userFileRecord);
Expand Down Expand Up @@ -172,7 +170,7 @@ export class FilesService {
});
}

async delete(fileID: ObjectId, location: string): Promise<string> {
async delete(userID: ObjectId, fileID: ObjectId, location: string): Promise<string> {
const file = await this.fileModel.findById(fileID.toString());
if(!file) {
this.loggerService.error(`File with ID ${fileID} does not exist`)
Expand All @@ -196,7 +194,7 @@ export class FilesService {
if(location == 'db' || location == 'all'){
// Deleting from db
try{
await this.userService.deleteUsersFile(fileID);
await this.userService.deleteUsersFile(userID, fileID, file.size);
await this.fileModel.findByIdAndDelete(fileID);
this.loggerService.info(`File deleted from db: ${fileID}`);
}catch(error){
Expand Down
3 changes: 3 additions & 0 deletions server/src/files/schema/file.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export const FileSchema = new mongoose.Schema({
pub_key:{
type: String,
},
size:{
type: Number,
},
ownerID:{
type: mongoose.Schema.Types.ObjectId,
required: true,
Expand Down
19 changes: 18 additions & 1 deletion server/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { ObjectId } from "mongodb";

export interface IUser extends Document {
_id: ObjectId
username?: string
username: string
email: string
password: string
storage?: number
activity?: IUserActivityRecord[]
files?: IUserFileRecord[]
createdAt: Date
}
Expand All @@ -16,16 +18,31 @@ export enum IRole{
OWNER = 'owner',
}

export enum IActivityAction{
LOGIN = 'login',
REGISTER = 'register',
LOGOUT = 'logout',
UPLOAD = 'upload',
DELETE = 'delete',
}

export interface IUserFileRecord{
originalname: string
fileID: ObjectId
size?: number
role: IRole[]
}

export interface IUserActivityRecord{
time: Date
action: IActivityAction
}

export interface IFile{
originalname: string
uuid: string
pub_key: string
size?: number
ownerID: ObjectId
gems?:{
index: number
Expand Down
17 changes: 17 additions & 0 deletions server/src/user/schema/user.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,25 @@ export const userFileRecord = new mongoose.Schema({
ref: 'Files',
required: true,
},
size:{
type: Number
},
role: [{
type: String,
enum: [IRole.VIEWER, IRole.EDITOR, IRole.OWNER],
required: true,
}]
});

export const userActivityRecord = new mongoose.Schema({
time:{
type: Date,
},
action:{
type: String,
},
});

export const UserSchema = new mongoose.Schema({
username:{
type:String,
Expand All @@ -34,6 +46,11 @@ export const UserSchema = new mongoose.Schema({
type:String,
required:true
},
storage:{
type:Number,
default: 0,
},
activity: [userActivityRecord],
files:[userFileRecord],
createdAt: {
type: Date,
Expand Down
26 changes: 20 additions & 6 deletions server/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@ import { ObjectId } from 'mongodb';
import * as bcrypt from 'bcrypt';
import { Model } from 'mongoose';
import { LoginDTO, RegisterDTO } from '../authentication/dto/auth.dto';
import { IPayload, IUser } from 'src/interfaces';
import { IPayload, IUser, IActivityAction } from 'src/interfaces';

@Injectable()
export class UserService {
constructor( @InjectModel('User') private userModel: Model<IUser>) {}

async create(registerDTO: RegisterDTO) : Promise<IUser> {
const { email } = registerDTO;
const user = await this.userModel.findOne({ email });
const { username } = registerDTO;
const user = await this.userModel.findOne({ username });
if(user){
throw new HttpException('User already exists with given email', HttpStatus.BAD_REQUEST);
throw new HttpException('User already exists with given username', HttpStatus.BAD_REQUEST);
}
const createdUser : IUser = new this.userModel(registerDTO);
const activityRecord = {
time: new Date(),
action: IActivityAction.REGISTER
}
createdUser.storage = 0;
createdUser.activity.push(activityRecord)
return await createdUser.save();
}

Expand Down Expand Up @@ -57,16 +63,24 @@ export class UserService {
}

async addFileToUser(userID: string, file: any){
const user = await this.userModel.findById(userID);
const updatedStorage = user.storage + file.size;
await this.userModel.updateOne(
{ _id: userID },
{ $push: { files: file } }
{ $push: { files: file }, $set: { storage: updatedStorage } }
);
}

async deleteUsersFile(fileID: ObjectId){
async deleteUsersFile(userID: ObjectId, fileID: ObjectId, size: number){
await this.userModel.updateMany(
{ files: { $elemMatch: { fileID: fileID } } },
{ $pull: { files: { fileID: fileID } } }
);
const user = await this.userModel.findById(userID);
const updatedStorage = user.storage - size;
await this.userModel.updateOne(
{ _id: userID },
{ $set: {storage: updatedStorage} }
);
}
}
21 changes: 16 additions & 5 deletions webv2/src/services/app.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,22 @@ import axios from 'axios';

class AppService {
async getTestContent() {
return axios.get(process.env.REACT_APP_API_URL);
return await axios.get(process.env.REACT_APP_API_URL);
}

async getUser(userID, authToken){
return await axios.get(process.env.REACT_APP_API_URL + 'user',{
headers: {
Authorization: `Bearer ${authToken}`
},
params:{
userID
}
})
}

async getUserFiles(userID, authToken) {
return axios.get(process.env.REACT_APP_API_URL + 'files/user', {
return await axios.get(process.env.REACT_APP_API_URL + 'files/user', {
headers: {
Authorization: `Bearer ${authToken}`
},
Expand All @@ -17,7 +28,7 @@ class AppService {
}

async uploadFile(file, userID, authToken) {
return axios.post(process.env.REACT_APP_API_URL + 'files/upload', file, {
return await axios.post(process.env.REACT_APP_API_URL + 'files/upload', file, {
headers: {
'Content-Type': 'multipart/form-data',
Authorization: `Bearer ${authToken}`
Expand All @@ -29,7 +40,7 @@ class AppService {
}

async downloadFile(fileID, authToken) {
return axios.get(process.env.REACT_APP_API_URL + 'files/download', {
return await axios.get(process.env.REACT_APP_API_URL + 'files/download', {
headers: {
Authorization: `Bearer ${authToken}`
},
Expand All @@ -41,7 +52,7 @@ class AppService {
}

async deleteFile(userID, fileID, authToken) {
return axios.delete(process.env.REACT_APP_API_URL + 'files', {
return await axios.delete(process.env.REACT_APP_API_URL + 'files', {
headers: {
Authorization: `Bearer ${authToken}`
},
Expand Down
7 changes: 3 additions & 4 deletions webv2/src/services/auth.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios from "axios";

class AuthService {
async login(username, password) {
return axios
return await axios
.post(`${process.env.REACT_APP_API_URL}` + "auth/login", {
username,
password
Expand All @@ -20,12 +20,11 @@ class AuthService {
localStorage.removeItem("user");
}

async register(user) {
console.log(user)
async register(user){
const username = user.username;
const password = user.password;
const email = user.email;
return axios.post(process.env.REACT_APP_API_URL + "auth/register", {
return await axios.post(process.env.REACT_APP_API_URL + "auth/register", {
username,
email,
password
Expand Down
9 changes: 7 additions & 2 deletions webv2/src/views/dashboard/Files.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import PageContainer from 'src/components/container/PageContainer';
import DashboardCard from '../../components/shared/DashboardCard';
import AppService from '../../services/app.service'
import AuthService from '../../services/auth.service'
import {Typography,Table,TableBody,TableCell,TableHead,TableRow,Chip,Button, Grid} from '@mui/material';
import {Typography,Table,TableBody,TableCell,TableHead,TableRow,Chip,Button,Grid, CircularProgress} from '@mui/material';
import CloudUploadIcon from '@mui/icons-material/CloudUpload';

const Files = () => {
const [files , setFiles] = useState([])
const [user , setUser] = useState()
const [selectedFile, setSelectedFile] = useState(null);
const [uploadFlag, setUploadFlag] = useState(false);
let currentUser;

useEffect(() => {
Expand Down Expand Up @@ -65,13 +66,14 @@ const Files = () => {
};

const handleUpload = (e) => {
console.log(user._id)
setUploadFlag(true);
const data = new FormData()
data.append('file', selectedFile)
AppService.uploadFile(data, user._id, JSON.parse(localStorage.getItem("user")).token).then(
response => {
const updatedFiles = [...files, response.data];
setFiles(updatedFiles);
setUploadFlag(false)
setSelectedFile(null);
},
error => {
Expand All @@ -89,6 +91,9 @@ const Files = () => {
<Typography>All of the uploaded files appear here</Typography>
</Grid>
<Grid>
{uploadFlag ?
<CircularProgress size={20} style={{ marginRight: 10 }} /> : <></>
}
<input
id="select-file"
type="file"
Expand Down
Loading

0 comments on commit a8b4911

Please sign in to comment.