diff --git a/jambotron-ui/src/app/modules/user-module/dialog-select-image.component/dialog-select-image.component.scss b/jambotron-ui/src/app/modules/user-module/dialog-select-image.component/dialog-select-image.component.scss
index ad0d4e1..6a02f8a 100644
--- a/jambotron-ui/src/app/modules/user-module/dialog-select-image.component/dialog-select-image.component.scss
+++ b/jambotron-ui/src/app/modules/user-module/dialog-select-image.component/dialog-select-image.component.scss
@@ -2,20 +2,30 @@
flex: 1 1 auto;
}
-.mat-dialog-content{
+.mat-mdc-list-item{
+
+}
+a.mdc-list-item
+{
+ cursor: grab;
+ height: 120px;
+ padding-bottom: 66px;
+ background-color: rgba(24,255,255,0.04);
+}
+/*.mat-dialog-content{
min-height: 300px;
min-width: 300px;
height: 75%;
width: 75%;
-}
-.mdc-dialog--open .mat-mdc-dialog-inner-container
+}*/
+/*.mdc-dialog--open .mat-mdc-dialog-inner-container
{
opacity: 1;
width: 600px;
-}
+}*/
-.mat-mdc-dialog-container {
+/*.mat-mdc-dialog-container {
width: 600px;
height: 500px;
display: block;
@@ -25,4 +35,9 @@
min-width: inherit;
max-width: inherit;
outline: 0;
+}*/
+/*
+.mat-mdc-dialog-content{
+ overflow: auto;
}
+*/
diff --git a/jambotron-ui/src/app/modules/user-module/dialog-select-image.component/dialog-select-image.component.ts b/jambotron-ui/src/app/modules/user-module/dialog-select-image.component/dialog-select-image.component.ts
index d030b1a..3dce61e 100644
--- a/jambotron-ui/src/app/modules/user-module/dialog-select-image.component/dialog-select-image.component.ts
+++ b/jambotron-ui/src/app/modules/user-module/dialog-select-image.component/dialog-select-image.component.ts
@@ -7,8 +7,7 @@ import {
MatDialogTitle
} from '@angular/material/dialog';
import {MatButton} from '@angular/material/button';
-import {MatListItem, MatNavList} from '@angular/material/list';
-import {RouterLink} from '@angular/router';
+import {MatListItem} from '@angular/material/list';
import {FileInfo} from '../../../models/file-info';
import {UserApiService} from '../user-api.service';
@@ -20,10 +19,7 @@ import {UserApiService} from '../user-api.service';
MatButton,
MatDialogActions,
MatDialogClose,
-
- MatListItem,
- MatNavList,
-
+ MatListItem
],
templateUrl: './dialog-select-image.component.html',
styleUrl: './dialog-select-image.component.scss'
@@ -42,10 +38,10 @@ export class DialogSelectImageComponent {
console.log(data);
});
}
+
+ //open upload dialog
upload() {
-
this.uploadClicked.emit();
-
}
onNoClick() {
@@ -54,5 +50,6 @@ export class DialogSelectImageComponent {
select(fileInfo:FileInfo) {
this.selectClicked.emit(fileInfo);
+ this.dialogRef.close();
}
}
diff --git a/jambotron-ui/src/app/modules/user-module/dialog-upload-image.component/dialog-upload-image.component.html b/jambotron-ui/src/app/modules/user-module/dialog-upload-image.component/dialog-upload-image.component.html
new file mode 100644
index 0000000..07b03c7
--- /dev/null
+++ b/jambotron-ui/src/app/modules/user-module/dialog-upload-image.component/dialog-upload-image.component.html
@@ -0,0 +1,14 @@
+
dialog-upload-image.component works!
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/jambotron-ui/src/app/modules/user-module/dialog-upload-image.component/dialog-upload-image.component.scss b/jambotron-ui/src/app/modules/user-module/dialog-upload-image.component/dialog-upload-image.component.scss
new file mode 100644
index 0000000..827c30d
--- /dev/null
+++ b/jambotron-ui/src/app/modules/user-module/dialog-upload-image.component/dialog-upload-image.component.scss
@@ -0,0 +1,6 @@
+.menu-spacer {
+ flex: 1 1 auto;
+}
+
+
+
diff --git a/jambotron-ui/src/app/modules/user-module/dialog-upload-image.component/dialog-upload-image.component.spec.ts b/jambotron-ui/src/app/modules/user-module/dialog-upload-image.component/dialog-upload-image.component.spec.ts
new file mode 100644
index 0000000..7e0f85f
--- /dev/null
+++ b/jambotron-ui/src/app/modules/user-module/dialog-upload-image.component/dialog-upload-image.component.spec.ts
@@ -0,0 +1,23 @@
+import { ComponentFixture, TestBed } from '@angular/core/testing';
+
+import { DialogUploadImageComponent } from './dialog-upload-image.component';
+
+describe('DialogUploadImageComponent', () => {
+ let component: DialogUploadImageComponent;
+ let fixture: ComponentFixture
;
+
+ beforeEach(async () => {
+ await TestBed.configureTestingModule({
+ imports: [DialogUploadImageComponent]
+ })
+ .compileComponents();
+
+ fixture = TestBed.createComponent(DialogUploadImageComponent);
+ component = fixture.componentInstance;
+ fixture.detectChanges();
+ });
+
+ it('should create', () => {
+ expect(component).toBeTruthy();
+ });
+});
diff --git a/jambotron-ui/src/app/modules/user-module/dialog-upload-image.component/dialog-upload-image.component.ts b/jambotron-ui/src/app/modules/user-module/dialog-upload-image.component/dialog-upload-image.component.ts
new file mode 100644
index 0000000..45ba8a7
--- /dev/null
+++ b/jambotron-ui/src/app/modules/user-module/dialog-upload-image.component/dialog-upload-image.component.ts
@@ -0,0 +1,54 @@
+import {Component, EventEmitter, inject, Output, ViewChild} from '@angular/core';
+import {FileUploadComponent} from '../../../components/file-upload.component/file-upload.component';
+import {MatDialogActions, MatDialogClose, MatDialogContent, MatDialogRef} from '@angular/material/dialog';
+import {MatButton} from '@angular/material/button';
+import {FileInfo} from '../../../models/file-info';
+
+@Component({
+ selector: 'app-dialog-upload-image.component',
+ imports: [
+ FileUploadComponent,
+ MatDialogContent,
+ MatButton,
+ MatDialogActions,
+ MatDialogClose
+ ],
+ templateUrl: './dialog-upload-image.component.html',
+ styleUrl: './dialog-upload-image.component.scss'
+})
+export class DialogUploadImageComponent {
+ //select uploaded file
+ @Output() selectUploadedImageClicked = new EventEmitter();
+
+ //open select dialog
+ @Output() openSelectDialogClicked = new EventEmitter();
+
+ readonly dialogRef = inject(MatDialogRef);
+
+ @ViewChild('imageUpload') imageUpload: FileUploadComponent | undefined;
+
+ imageUploaded:boolean = false;
+
+ onNoClick() {
+ this.dialogRef.close();
+ }
+
+ selectUploadedImage() {
+ this.selectUploadedImageClicked.emit(this.imageUpload?.fileInfo?.url);
+ this.dialogRef.close();
+ }
+
+ openSelectImageDialog() {
+ this.openSelectDialogClicked.emit();
+ this.dialogRef.close();
+ }
+
+ imageUpload_ImageUploaded($event: FileInfo | undefined) {
+ if($event){
+ this.imageUploaded = true;
+ }
+ else{
+ this.imageUploaded = false;
+ }
+ }
+}
diff --git a/jambotron-ui/src/app/modules/user-module/tutorial-add.component/tutorial-add.component.html b/jambotron-ui/src/app/modules/user-module/tutorial-add.component/tutorial-add.component.html
index 6dacd62..b35dfde 100644
--- a/jambotron-ui/src/app/modules/user-module/tutorial-add.component/tutorial-add.component.html
+++ b/jambotron-ui/src/app/modules/user-module/tutorial-add.component/tutorial-add.component.html
@@ -14,9 +14,9 @@
-
-
+
+
Description
diff --git a/jambotron-ui/src/app/modules/user-module/tutorial-add.component/tutorial-add.component.ts b/jambotron-ui/src/app/modules/user-module/tutorial-add.component/tutorial-add.component.ts
index 34a29e9..bfa6464 100644
--- a/jambotron-ui/src/app/modules/user-module/tutorial-add.component/tutorial-add.component.ts
+++ b/jambotron-ui/src/app/modules/user-module/tutorial-add.component/tutorial-add.component.ts
@@ -12,10 +12,10 @@ import {MatFormField, MatInput, MatLabel} from '@angular/material/input';
import {MatTab, MatTabGroup} from '@angular/material/tabs';
import {FormBuilder, FormGroup, FormsModule, ReactiveFormsModule} from '@angular/forms';
import {AngularMarkdownEditorModule, EditorInstance, EditorOption} from 'angular-markdown-editor';
-import {DialogLoginComponent} from '../../main-module/main.component/dialog-login.component/dialog-login.component';
-import {GlobalConstants} from '../../../global-constants';
import {MatDialog} from '@angular/material/dialog';
import {DialogSelectImageComponent} from '../dialog-select-image.component/dialog-select-image.component';
+import {DialogUploadImageComponent} from '../dialog-upload-image.component/dialog-upload-image.component';
+import {FileUploadComponent} from '../../../components/file-upload.component/file-upload.component';
@Component({
selector: 'app-tutorial-add.component',
@@ -51,6 +51,8 @@ export class TutorialAddComponent implements OnInit{
readonly dialog = inject(MatDialog);
+
+
markdown = `## Markdown __rulez__!
---
@@ -72,10 +74,6 @@ const language = 'typescript';
constructor(private fb: FormBuilder,
private markdownService: MarkdownService,
private userApiService: UserApiService,
- private storageService: TokenStorageService,
- private eventBusService: EventBusService,
- private router: Router,
- private authService: AuthService
) {
this.tutorial.description = this.markdown;
@@ -185,7 +183,7 @@ const language = 'typescript';
});
dialogSelectRef.componentInstance.uploadClicked.subscribe(result => {
dialogSelectRef.close();
- //this.openSignupDialog(enterAnimationDuration, exitAnimationDuration);
+ this.openUploadImageDialog(enterAnimationDuration, exitAnimationDuration);
})
const dialogSelectSubscription = dialogSelectRef.componentInstance.selectClicked
@@ -199,4 +197,29 @@ const language = 'typescript';
});
}
+
+ openUploadImageDialog(enterAnimationDuration: string, exitAnimationDuration: string) {
+ let dialogSelectRef = this.dialog.open(DialogUploadImageComponent, {
+ height: '500px',
+ width: '600px',
+ enterAnimationDuration,
+ exitAnimationDuration,
+ // data: {username: this.dialogLoginData.username, password: this.dialogLoginData.password}
+ });
+ dialogSelectRef.componentInstance.openSelectDialogClicked.subscribe(result => {
+ dialogSelectRef.close();
+ this.openSelectImageDialog(enterAnimationDuration, exitAnimationDuration);
+ })
+
+ const dialogUploadSubscription = dialogSelectRef.componentInstance.selectUploadedImageClicked
+ .subscribe(result => {
+ console.log('Got the data!', result);
+
+ if (result == null) {
+ return;
+ }
+ this.tutorial.titleimage = result.url;
+
+ });
+ }
}
diff --git a/jambotron-ui/src/app/modules/user-module/user-api.service.ts b/jambotron-ui/src/app/modules/user-module/user-api.service.ts
index 0675f18..8dfda62 100644
--- a/jambotron-ui/src/app/modules/user-module/user-api.service.ts
+++ b/jambotron-ui/src/app/modules/user-module/user-api.service.ts
@@ -1,7 +1,7 @@
import { Injectable } from '@angular/core';
import {forkJoin, Observable} from 'rxjs';
import {Tutorial} from '../../models/tutorial.model';
-import {HttpClient, HttpHeaders} from '@angular/common/http';
+import {HttpClient, HttpEvent, HttpHeaders, HttpRequest} from '@angular/common/http';
import {GlobalConstants} from '../../global-constants';
import {FileInfo} from '../../models/file-info';
@@ -15,6 +15,19 @@ export class UserApiService {
}
+ upload(file: File): Observable> {
+ const formData: FormData = new FormData();
+ formData.append('file', file);
+
+ const req = new HttpRequest('POST', `${this.baseUrl}/upload`, formData, {
+ reportProgress: true,
+ responseType: 'json'
+ });
+
+ return this.http.request(req);
+ }
+
+
saveZhipuaiImage(url: string): Observable {
return this.http.post(`${this.baseUrl}/saveZhipuAiImage`, {imageUrl:url});
}
diff --git a/jambotron-ui/src/app/modules/user-module/user.module.ts b/jambotron-ui/src/app/modules/user-module/user.module.ts
index 93853ae..a0df98e 100644
--- a/jambotron-ui/src/app/modules/user-module/user.module.ts
+++ b/jambotron-ui/src/app/modules/user-module/user.module.ts
@@ -3,6 +3,7 @@ import { CommonModule } from '@angular/common';
import {userRouting} from './user.routing';
import {UserComponent} from './user.component/user.component';
import {AngularMarkdownEditorModule} from 'angular-markdown-editor';
+import {MatFormFieldModule} from '@angular/material/form-field';
@@ -12,7 +13,8 @@ import {AngularMarkdownEditorModule} from 'angular-markdown-editor';
userRouting,
UserComponent,
CommonModule,
- AngularMarkdownEditorModule
+ AngularMarkdownEditorModule,
+ MatFormFieldModule
]
})
export class UserModule { }
diff --git a/jambotron-ui/src/app/services/file-upload-api.service.spec.ts b/jambotron-ui/src/app/services/file-upload-api.service.spec.ts
deleted file mode 100644
index bc34a28..0000000
--- a/jambotron-ui/src/app/services/file-upload-api.service.spec.ts
+++ /dev/null
@@ -1,16 +0,0 @@
-import { TestBed } from '@angular/core/testing';
-
-import { FileUploadApiService } from './file-upload-api.service';
-
-describe('FileUploadApiService', () => {
- let service: FileUploadApiService;
-
- beforeEach(() => {
- TestBed.configureTestingModule({});
- service = TestBed.inject(FileUploadApiService);
- });
-
- it('should be created', () => {
- expect(service).toBeTruthy();
- });
-});
diff --git a/jambotron-ui/src/app/services/file-upload-api.service.ts b/jambotron-ui/src/app/services/file-upload-api.service.ts
deleted file mode 100644
index 06c48de..0000000
--- a/jambotron-ui/src/app/services/file-upload-api.service.ts
+++ /dev/null
@@ -1,29 +0,0 @@
-import { Injectable } from '@angular/core';
-import {GlobalConstants} from '../global-constants';
-import {HttpClient, HttpEvent, HttpRequest} from '@angular/common/http';
-import {Observable} from 'rxjs';
-import {FileInfo} from '../models/file-info';
-
-@Injectable({
- providedIn: 'root'
-})
-export class FileUploadService {
- private baseUrl = GlobalConstants.API_URL + '/file';
-
- constructor(private http: HttpClient) {}
-
- upload(file: File): Observable> {
- const formData: FormData = new FormData();
- formData.append('file', file);
-
- const req = new HttpRequest('POST', `${this.baseUrl}/upload`, formData, {
- responseType: 'json',
- });
-
- return this.http.request(req);
- }
-
- getFiles(): Observable {
- return this.http.get(`${this.baseUrl}/files`);
- }
-}
diff --git a/src/main/java/com/jambotronGroup/jambotron/JambotronApplication.java b/src/main/java/com/jambotronGroup/jambotron/JambotronApplication.java
index 413376c..369760e 100644
--- a/src/main/java/com/jambotronGroup/jambotron/JambotronApplication.java
+++ b/src/main/java/com/jambotronGroup/jambotron/JambotronApplication.java
@@ -5,15 +5,9 @@ import com.jambotronGroup.jambotron.fileUpload.FilesStorageService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.slf4j.Marker;
-import org.slf4j.event.Level;
-import org.slf4j.helpers.BasicMarker;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.context.annotation.Import;
-
-import java.util.Iterator;
@SpringBootApplication
public class JambotronApplication implements CommandLineRunner {
@@ -35,4 +29,5 @@ public class JambotronApplication implements CommandLineRunner {
// storageService.deleteAll();
storageService.init();
}
+
}
diff --git a/src/main/java/com/jambotronGroup/jambotron/configuretions/MvcConfig.java b/src/main/java/com/jambotronGroup/jambotron/configuretions/MvcConfig.java
deleted file mode 100644
index a637531..0000000
--- a/src/main/java/com/jambotronGroup/jambotron/configuretions/MvcConfig.java
+++ /dev/null
@@ -1,25 +0,0 @@
-package com.jambotronGroup.jambotron.configuretions;
-
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.web.servlet.config.annotation.EnableWebMvc;
-import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
-import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
-/*
-@Configuration
-
-@EnableWebMvc
-public class MvcConfig implements WebMvcConfigurer {
- @Value("${spring.resources.static-locations}")
- String resourceLocations;
-
- @Override
- public void addResourceHandlers(ResourceHandlerRegistry registry) {
- //registry.addResourceHandler("/public/**").addResourceLocations(resourceLocations);
- registry.addResourceHandler("/**").addResourceLocations("classpath:/resources/");
-
- //registry.
- registry.addResourceHandler("/media/**").addResourceLocations("resources/main/public/media/");
-
- }
-}*/
diff --git a/src/main/java/com/jambotronGroup/jambotron/configuretions/NotFoundHandler.java b/src/main/java/com/jambotronGroup/jambotron/configuretions/NotFoundHandler.java
deleted file mode 100644
index df66fd9..0000000
--- a/src/main/java/com/jambotronGroup/jambotron/configuretions/NotFoundHandler.java
+++ /dev/null
@@ -1,35 +0,0 @@
-package com.jambotronGroup.jambotron.configuretions;
-
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.MediaType;
-import org.springframework.http.ResponseEntity;
-import org.springframework.util.ResourceUtils;
-import org.springframework.util.StreamUtils;
-import org.springframework.web.bind.annotation.ControllerAdvice;
-import org.springframework.web.bind.annotation.ExceptionHandler;
-import org.springframework.web.servlet.NoHandlerFoundException;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.nio.charset.Charset;
-/*
-@ControllerAdvice
-public class NotFoundHandler {
- @Value("${spa.default-file}")
- String defaultFile;
-
- @ExceptionHandler(NoHandlerFoundException.class)
- public ResponseEntity renderDefaultPage() {
- try {
- File indexFile = ResourceUtils.getFile(defaultFile);
- FileInputStream inputStream = new FileInputStream(indexFile);
- String body = StreamUtils.copyToString(inputStream, Charset.defaultCharset());
- return ResponseEntity.ok().contentType(MediaType.TEXT_HTML).body(body);
- } catch (IOException e) {
- e.printStackTrace();
- return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("There was an error completing the action.");
- }
- }
-}*/
diff --git a/src/main/java/com/jambotronGroup/jambotron/controllers/FilesController.java b/src/main/java/com/jambotronGroup/jambotron/controllers/FilesController.java
index b47f4d8..854ae34 100644
--- a/src/main/java/com/jambotronGroup/jambotron/controllers/FilesController.java
+++ b/src/main/java/com/jambotronGroup/jambotron/controllers/FilesController.java
@@ -1,6 +1,7 @@
package com.jambotronGroup.jambotron.controllers;
import com.jambotronGroup.jambotron.fileUpload.FilesStorageService;
+import com.jambotronGroup.jambotron.fileUpload.ResponseImageUploadResult;
import com.jambotronGroup.jambotron.fileUpload.ResponseMessage;
import com.jambotronGroup.jambotron.model.FileInfo;
import com.jambotronGroup.jambotron.model.User;
@@ -30,17 +31,32 @@ import java.util.List;
import java.util.stream.Collectors;
@Controller
-//@CrossOrigin("http://localhost:8081")
public class FilesController {
private static final Logger logger = LoggerFactory.getLogger(FilesController.class);
-
@Autowired
AuthenticationFacade authenticationFacade;
@Autowired
FilesStorageService storageService;
+ /**
+ * Generates a URL for accessing a user's image.
+ * format: /api/user/user-images/{filename:.+}
+ *
+ * @param filename The name of the file.
+ * @return The URL to access the file.
+ */
+ private static String getUserImageUrl(String filename) {
+ return MvcUriComponentsBuilder
+ .fromMethodName(FilesController.class, "getUserImage", filename).build().toString();
+ }
+
+ private static String getPublicImageUrl(String filename) {
+ return MvcUriComponentsBuilder
+ .fromMethodName(FilesController.class, "getFile", filename).build().toString();
+ }
+
@PostMapping("/api/user/saveZhipuAiImage")
public ResponseEntity saveZhipuAiImage(@Valid @RequestBody SaveZhipuAiImageRequest request) {
@@ -52,7 +68,6 @@ public class FilesController {
String message = "";
-
RestTemplate restTemplate = new RestTemplate();
try {
@@ -92,14 +107,21 @@ public class FilesController {
return ResponseEntity.status(HttpStatus.OK).body(new ResponseMessage(message));
}
- @PostMapping("/api/file/upload")
+ @PostMapping("/api/user/upload")
public ResponseEntity uploadFile(@RequestParam("file") MultipartFile file) {
String message = "";
try {
- storageService.save(file);
+ User user = authenticationFacade.getUser();
+
+ String localFullFileName = storageService.save(user.getId().toString(), file);
+
+ FileInfo fileInfo = new FileInfo(
+ file.getOriginalFilename(),
+ FilesController.getUserImageUrl(localFullFileName));
+
message = "Uploaded the file successfully: " + file.getOriginalFilename();
- return ResponseEntity.status(HttpStatus.OK).body(new ResponseMessage(message));
+ return ResponseEntity.status(HttpStatus.OK).body(new ResponseImageUploadResult(message,fileInfo));
} catch (Exception e) {
message = "Could not upload the file: " + file.getOriginalFilename() + ". Error: " + e.getMessage();
return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ResponseMessage(message));
@@ -113,8 +135,8 @@ public class FilesController {
List fileInfos = storageService.loadUserImages(user.getId().toString()).map(path -> {
String filename = path.getFileName().toString();
- String url = MvcUriComponentsBuilder
- .fromMethodName(FilesController.class, "getUserImage", path.getFileName().toString()).build().toString();
+
+ String url = FilesController.getUserImageUrl(filename);
return new FileInfo(filename, url);
}).collect(Collectors.toList());
@@ -127,9 +149,7 @@ public class FilesController {
public ResponseEntity> getListFiles() {
List fileInfos = storageService.loadAll().map(path -> {
String filename = path.getFileName().toString();
- String url = MvcUriComponentsBuilder
- .fromMethodName(FilesController.class, "getFile", path.getFileName().toString()).build().toString();
-
+ String url = FilesController.getPublicImageUrl(filename);
return new FileInfo(filename, url);
}).collect(Collectors.toList());
diff --git a/src/main/java/com/jambotronGroup/jambotron/exceptionHandlers/FileUploadExceptionHandler.java b/src/main/java/com/jambotronGroup/jambotron/exceptionHandlers/FileUploadExceptionHandler.java
new file mode 100644
index 0000000..9e69a0c
--- /dev/null
+++ b/src/main/java/com/jambotronGroup/jambotron/exceptionHandlers/FileUploadExceptionHandler.java
@@ -0,0 +1,18 @@
+package com.jambotronGroup.jambotron.exceptionHandlers;
+
+import com.jambotronGroup.jambotron.fileUpload.ResponseMessage;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.multipart.MaxUploadSizeExceededException;
+import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
+
+//@ControllerAdvice
+//public class FileUploadExceptionHandler extends ResponseEntityExceptionHandler {
+//
+// @ExceptionHandler(MaxUploadSizeExceededException.class)
+// public ResponseEntity handleMaxSizeException(MaxUploadSizeExceededException exc) {
+// return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ResponseMessage("File too large!"));
+// }
+//}
\ No newline at end of file
diff --git a/src/main/java/com/jambotronGroup/jambotron/fileUpload/FilesStorageService.java b/src/main/java/com/jambotronGroup/jambotron/fileUpload/FilesStorageService.java
index 606f005..4acd984 100644
--- a/src/main/java/com/jambotronGroup/jambotron/fileUpload/FilesStorageService.java
+++ b/src/main/java/com/jambotronGroup/jambotron/fileUpload/FilesStorageService.java
@@ -10,7 +10,7 @@ import java.util.stream.Stream;
public interface FilesStorageService {
public void init();
- public void save(String userID,MultipartFile file);
+ public String save(String userID,MultipartFile file);
public void save(MultipartFile file);
diff --git a/src/main/java/com/jambotronGroup/jambotron/fileUpload/FilesStorageServiceImpl.java b/src/main/java/com/jambotronGroup/jambotron/fileUpload/FilesStorageServiceImpl.java
index aae255c..404c847 100644
--- a/src/main/java/com/jambotronGroup/jambotron/fileUpload/FilesStorageServiceImpl.java
+++ b/src/main/java/com/jambotronGroup/jambotron/fileUpload/FilesStorageServiceImpl.java
@@ -56,14 +56,25 @@ public class FilesStorageServiceImpl implements FilesStorageService {
return this.rootPublic.resolve(newFilename);
}
+ /**
+ * Saves a file to a user's directory.
+ * uploads/user-images/{userID}/{filename}
+ *
+ * @param userID The ID of the user.
+ * @param file The file to save.
+ */
@Override
- public void save(String userID,MultipartFile file) {
+ public String save(String userID,MultipartFile file) {
+ Path targetPath = null;
try {
Path path = this.root.resolve(userID);
path.toFile().mkdirs(); // Ensure user directory exists
- Files.copy(file.getInputStream(), path.resolve(file.getOriginalFilename()),
+ targetPath = path.resolve(file.getOriginalFilename());
+
+ Files.copy(file.getInputStream(), targetPath,
java.nio.file.StandardCopyOption.REPLACE_EXISTING);
+
} catch (Exception e) {
if (e instanceof FileAlreadyExistsException) {
throw new RuntimeException("A file of that name already exists.");
@@ -71,6 +82,8 @@ public class FilesStorageServiceImpl implements FilesStorageService {
throw new RuntimeException(e.getMessage());
}
+
+ return targetPath.getFileName().toString();
}
@Override
diff --git a/src/main/java/com/jambotronGroup/jambotron/fileUpload/ResponseImageUploadResult.java b/src/main/java/com/jambotronGroup/jambotron/fileUpload/ResponseImageUploadResult.java
new file mode 100644
index 0000000..26d2f09
--- /dev/null
+++ b/src/main/java/com/jambotronGroup/jambotron/fileUpload/ResponseImageUploadResult.java
@@ -0,0 +1,21 @@
+package com.jambotronGroup.jambotron.fileUpload;
+
+import com.jambotronGroup.jambotron.model.FileInfo;
+
+public class ResponseImageUploadResult extends ResponseMessage {
+ private FileInfo fileInfo;
+
+
+ public ResponseImageUploadResult(String message, FileInfo fileInfo) {
+ super(message);
+ this.fileInfo = fileInfo;
+ }
+
+ public FileInfo getFileInfo() {
+ return this.fileInfo;
+ }
+
+ public void setFileInfo(FileInfo fileInfo) {
+ this.fileInfo = fileInfo;
+ }
+}
diff --git a/src/main/java/com/jambotronGroup/jambotron/fileUpload/ResponseMessage.java b/src/main/java/com/jambotronGroup/jambotron/fileUpload/ResponseMessage.java
index 978fb01..d10fc55 100644
--- a/src/main/java/com/jambotronGroup/jambotron/fileUpload/ResponseMessage.java
+++ b/src/main/java/com/jambotronGroup/jambotron/fileUpload/ResponseMessage.java
@@ -3,6 +3,8 @@ package com.jambotronGroup.jambotron.fileUpload;
public class ResponseMessage {
private String message;
+
+
public ResponseMessage(String message) {
this.message = message;
}
diff --git a/src/main/java/com/jambotronGroup/jambotron/security/WebSecurityConfig.java b/src/main/java/com/jambotronGroup/jambotron/security/WebSecurityConfig.java
index 19ccd31..08bda7f 100644
--- a/src/main/java/com/jambotronGroup/jambotron/security/WebSecurityConfig.java
+++ b/src/main/java/com/jambotronGroup/jambotron/security/WebSecurityConfig.java
@@ -104,6 +104,9 @@ public class WebSecurityConfig implements WebMvcConfigurer {// extends WebSecuri
.requestMatchers("/tutorials-images/**").permitAll()
+ .requestMatchers("/api/file/files").permitAll()
+ .requestMatchers("/api/file/upload").permitAll()
+
.requestMatchers("/files/**").permitAll()
.requestMatchers("/api/auth/**").permitAll()