UploadAPI and profilePicture #102
@ -12,7 +12,7 @@ import java.util.Arrays;
 | 
				
			|||||||
import java.util.Date;
 | 
					import java.util.Date;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@RestController
 | 
					@RestController
 | 
				
			||||||
@CrossOrigin(origins = "http://localhost:5173")
 | 
					@CrossOrigin(originPatterns = "*", allowCredentials = "true")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class MockController {
 | 
					public class MockController {
 | 
				
			||||||
    private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
 | 
					    private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
 | 
				
			||||||
 | 
				
			|||||||
@ -7,7 +7,7 @@ import org.springframework.web.bind.annotation.RestController;
 | 
				
			|||||||
import ovh.herisson.Clyde.Responses.PingResponse;
 | 
					import ovh.herisson.Clyde.Responses.PingResponse;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@RestController
 | 
					@RestController
 | 
				
			||||||
@CrossOrigin(origins = "http://localhost:5173")
 | 
					@CrossOrigin(originPatterns = "*", allowCredentials = "true")
 | 
				
			||||||
public class PingController {
 | 
					public class PingController {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@GetMapping("/ping")
 | 
						@GetMapping("/ping")
 | 
				
			||||||
 | 
				
			|||||||
@ -1,16 +1,15 @@
 | 
				
			|||||||
package ovh.herisson.Clyde.EndPoints;
 | 
					package ovh.herisson.Clyde.EndPoints;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import org.springframework.http.HttpHeaders;
 | 
					 | 
				
			||||||
import org.springframework.http.HttpStatus;
 | 
					import org.springframework.http.HttpStatus;
 | 
				
			||||||
import org.springframework.http.ResponseEntity;
 | 
					import org.springframework.http.ResponseEntity;
 | 
				
			||||||
import org.springframework.web.bind.annotation.*;
 | 
					import org.springframework.web.bind.annotation.*;
 | 
				
			||||||
import org.springframework.web.multipart.MultipartFile;
 | 
					import org.springframework.web.multipart.MultipartFile;
 | 
				
			||||||
import ovh.herisson.Clyde.Services.StorageService;
 | 
					import ovh.herisson.Clyde.Services.StorageService;
 | 
				
			||||||
import org.springframework.core.io.Resource;
 | 
					 | 
				
			||||||
import ovh.herisson.Clyde.Tables.FileType;
 | 
					import ovh.herisson.Clyde.Tables.FileType;
 | 
				
			||||||
 | 
					import ovh.herisson.Clyde.Tables.StorageFile;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@RestController
 | 
					@RestController
 | 
				
			||||||
@CrossOrigin(origins = "http://localhost:5173")
 | 
					@CrossOrigin(originPatterns = "*", allowCredentials = "true")
 | 
				
			||||||
public class StorageController {
 | 
					public class StorageController {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private final StorageService storageServ;
 | 
					    private final StorageService storageServ;
 | 
				
			||||||
@ -21,12 +20,17 @@ public class StorageController {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @PostMapping("/upload/{fileType}")
 | 
					    @PostMapping("/upload/{fileType}")
 | 
				
			||||||
    public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file, @PathVariable FileType fileType) {
 | 
					    public ResponseEntity<StorageFile> handleFileUpload(@RequestParam("file") MultipartFile file, @PathVariable FileType fileType) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        String path = storageServ.store(file,fileType);
 | 
							StorageFile fileEntry = null;
 | 
				
			||||||
 | 
							try {
 | 
				
			||||||
 | 
								fileEntry = storageServ.store(file,fileType);
 | 
				
			||||||
			
 | 
								
 | 
				
			||||||
        if (path == null) return new ResponseEntity<>("issue with the file storage", HttpStatus.BAD_REQUEST);
 | 
							} catch(Exception e){
 | 
				
			||||||
 | 
								e.printStackTrace();
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return  new ResponseEntity<>(path, HttpStatus.OK);
 | 
					
 | 
				
			||||||
 | 
					        return new ResponseEntity<>(fileEntry, HttpStatus.OK);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -9,7 +9,7 @@ import ovh.herisson.Clyde.Services.TokenService;
 | 
				
			|||||||
import ovh.herisson.Clyde.Tables.Token;
 | 
					import ovh.herisson.Clyde.Tables.Token;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@RestController
 | 
					@RestController
 | 
				
			||||||
@CrossOrigin(origins = "http://localhost:5173")
 | 
					@CrossOrigin(originPatterns = "*", allowCredentials = "true")
 | 
				
			||||||
public class TokenController {
 | 
					public class TokenController {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private final TokenService tokenServ;
 | 
					    private final TokenService tokenServ;
 | 
				
			||||||
 | 
				
			|||||||
@ -12,7 +12,7 @@ import ovh.herisson.Clyde.Tables.User;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@RestController
 | 
					@RestController
 | 
				
			||||||
@CrossOrigin(origins = "http://localhost:5173")
 | 
					@CrossOrigin(originPatterns = "*", allowCredentials = "true")
 | 
				
			||||||
public class UserController {
 | 
					public class UserController {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private final UserService userService;
 | 
					    private final UserService userService;
 | 
				
			||||||
 | 
				
			|||||||
@ -27,7 +27,7 @@ public class StorageService {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public String store(MultipartFile file, FileType fileType) {
 | 
					    public StorageFile store(MultipartFile file, FileType fileType) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (file.getOriginalFilename().isEmpty()){return null;}
 | 
					        if (file.getOriginalFilename().isEmpty()){return null;}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -49,9 +49,7 @@ public class StorageService {
 | 
				
			|||||||
        String url = this.rootLocation.resolve(Paths.get(Objects.requireNonNull(stringUuid)))
 | 
					        String url = this.rootLocation.resolve(Paths.get(Objects.requireNonNull(stringUuid)))
 | 
				
			||||||
                .normalize().toString();
 | 
					                .normalize().toString();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        fileRepo.save(new StorageFile(file.getName(),url, fileType));
 | 
					        return fileRepo.save(new StorageFile(file.getName(),url, fileType));
 | 
				
			||||||
 | 
					 | 
				
			||||||
        return url;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public void delete(StorageFile file) throws SecurityException {
 | 
					    public void delete(StorageFile file) throws SecurityException {
 | 
				
			||||||
 | 
				
			|||||||
@ -1,7 +1,9 @@
 | 
				
			|||||||
<script setup>
 | 
					<script setup>
 | 
				
			||||||
  import { login , register} from '@/rest/Users.js'
 | 
					 | 
				
			||||||
  import { ref } from 'vue'
 | 
					  import { ref } from 'vue'
 | 
				
			||||||
  import i18n from '@/i18n.js'
 | 
					  import i18n from '@/i18n.js'
 | 
				
			||||||
 | 
					  import { login , register } from '@/rest/Users.js'
 | 
				
			||||||
 | 
					  import { uploadProfilePicture } from '@/rest/uploads.js'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const loginPage= ref(true)
 | 
					  const loginPage= ref(true)
 | 
				
			||||||
  const page = ref(0)
 | 
					  const page = ref(0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -23,6 +25,8 @@
 | 
				
			|||||||
  const registerInfos= [{_surname:surname},{_firstname:firstname},{_birthday:birthday},{_passwordOUT:passwordOUT},
 | 
					  const registerInfos= [{_surname:surname},{_firstname:firstname},{_birthday:birthday},{_passwordOUT:passwordOUT},
 | 
				
			||||||
  {_passwordConfirm:passwordConfirm},{_emailOUT:emailOUT},{_address:address},{_country:country},{_cursus:cursus}]
 | 
					  {_passwordConfirm:passwordConfirm},{_emailOUT:emailOUT},{_address:address},{_country:country},{_cursus:cursus}]
 | 
				
			||||||
 
 | 
					 
 | 
				
			||||||
 | 
					  const imageSaved = ref(false)
 | 
				
			||||||
 | 
					  const ppData = ref(false)
 | 
				
			||||||
</script>
 | 
					</script>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -99,6 +103,10 @@
 | 
				
			|||||||
                <p>{{i18n("login.guest.country")}}</p>
 | 
					                <p>{{i18n("login.guest.country")}}</p>
 | 
				
			||||||
                <input type="text" v-model="country">
 | 
					                <input type="text" v-model="country">
 | 
				
			||||||
              </div>
 | 
					              </div>
 | 
				
			||||||
 | 
					              <form novalidate enctype="multipart/form-data" class="inputBox">
 | 
				
			||||||
 | 
					              	<p>ProfilePicture</p> 
 | 
				
			||||||
 | 
									<input type="file" :disabled="imageSaved" @change="ppData = uploadProfilePicture($event.target.files); imageSaved = true;" accept="image/*">
 | 
				
			||||||
 | 
					              </form>
 | 
				
			||||||
              <div class="inputBox">
 | 
					              <div class="inputBox">
 | 
				
			||||||
                <p>CURSUS</p> 
 | 
					                <p>CURSUS</p> 
 | 
				
			||||||
                  <select v-model="cursus">
 | 
					                  <select v-model="cursus">
 | 
				
			||||||
 | 
				
			|||||||
@ -11,6 +11,11 @@ export async function restPost(endPoint, data) {
 | 
				
			|||||||
	return await _rest(endPoint, {method: "POST", credentials: 'include', body: JSON.stringify(data)});
 | 
						return await _rest(endPoint, {method: "POST", credentials: 'include', body: JSON.stringify(data)});
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export async function restPostFile(endPoint, file){
 | 
				
			||||||
 | 
						let headers = new Headers();
 | 
				
			||||||
 | 
						return await _rest(endPoint, {method: "POST", credentials: 'include', body: file, headers: headers });
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export async function restDelete(endPoint, data) {
 | 
					export async function restDelete(endPoint, data) {
 | 
				
			||||||
	return await _rest(endPoint, {method: "DELETE", credentials: 'include', body: JSON.stringify(data)});
 | 
						return await _rest(endPoint, {method: "DELETE", credentials: 'include', body: JSON.stringify(data)});
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -35,7 +40,7 @@ async function _rest(endPoint, config){
 | 
				
			|||||||
		'Authorization': session_token,
 | 
							'Authorization': session_token,
 | 
				
			||||||
		'Content-Type': 'application/json',
 | 
							'Content-Type': 'application/json',
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
	config['headers'] = headers;
 | 
						config['headers'] = config['headers'] == null ? headers : config['headers'];
 | 
				
			||||||
	return toast.promise(fetch(restURL + endPoint, config),
 | 
						return toast.promise(fetch(restURL + endPoint, config),
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			pending: config['pending'] != null ? config['pending'] : 'pending',
 | 
								pending: config['pending'] != null ? config['pending'] : 'pending',
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										11
									
								
								frontend/src/rest/uploads.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								frontend/src/rest/uploads.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,11 @@
 | 
				
			|||||||
 | 
					import { restPostFile } from '@/rest/restConsumer.js'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Upload a file to the server and return the url of this image
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					export async function uploadProfilePicture(file){
 | 
				
			||||||
 | 
						const formData = new FormData();
 | 
				
			||||||
 | 
						formData.append("file", file[0]);
 | 
				
			||||||
 | 
						return restPostFile("/upload/ProfilePicture", formData)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user