upload file api

This commit is contained in:
Michele Artini 2022-11-23 14:45:56 +01:00
parent f3439a1d52
commit a49a9c9c01
3 changed files with 52 additions and 14 deletions

View File

@ -13,10 +13,13 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@ -47,18 +50,18 @@ public class ResourcesRestController {
}
@DeleteMapping("/{id}")
public void deleteResources(@PathVariable final String id) {
public void deleteResource(@PathVariable final String id) {
log.info("Deleting resource: " + id);
simpleResourceRepository.deleteById(id);
}
@GetMapping("/{id}/metadata")
public SimpleResource getMetadataResource(@PathVariable final String id) throws InformationServiceException {
public SimpleResource getMetadata(@PathVariable final String id) throws InformationServiceException {
return simpleResourceRepository.findById(id).orElseThrow(() -> new InformationServiceException("Id not found"));
}
@GetMapping("/{id}/content")
public void getContentResource(@PathVariable final String id, final HttpServletResponse res) throws InformationServiceException {
public void getContent(@PathVariable final String id, final HttpServletResponse res) throws InformationServiceException {
final SimpleResource sr = simpleResourceRepository.findById(id).orElseThrow(() -> new InformationServiceException("Id not found"));
res.setCharacterEncoding(StandardCharsets.UTF_8.name());
@ -73,12 +76,45 @@ public class ResourcesRestController {
}
}
@PostMapping(value = "/operation/import", consumes = "text/plain")
@PostMapping("/{id}/metadata")
public void saveMetadata(@RequestBody final SimpleResource r) throws InformationServiceException {
if (simpleResourceRepository.existsById(r.getId())) {
simpleResourceRepository.save(r);
} else {
throw new InformationServiceException("Resource not found");
}
}
@PostMapping("/{id}/content")
public void saveContent(@PathVariable final String id, @RequestParam final String content) throws InformationServiceException {
if (simpleResourceRepository.existsById(id)) {
simpleResourceRepository.setContentById(id, content);
} else {
throw new InformationServiceException("Resource not found");
}
}
@PutMapping(value = "/{id}/content", consumes = {
MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE
})
public void uploadContent(@PathVariable final String id, final HttpServletRequest request) throws InformationServiceException {
if (simpleResourceRepository.existsById(id)) {
try {
final String content = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8);
simpleResourceRepository.setContentById(id, content);
} catch (final Exception e) {
throw new InformationServiceException("Error processing file", e);
}
} else {
throw new InformationServiceException("Resource not found");
}
}
@PostMapping(value = "/operation/import", consumes = {
MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE
})
public SimpleResource importFromOldProfile(final HttpServletRequest request) throws Exception {
final String xml = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8);
return oldProfilesImporter.importSimpleResource(xml);
}
}

View File

@ -43,12 +43,12 @@ app.controller('resourcesController', function($scope, $http) {
$scope.tmpRes = angular.copy(res);
}
$scope.saveMetadata = function(res) {
$scope.saveMetadata = function(md) {
if ($scope.mode == 'new') {
var found = false;
angular.forEach($scope.resources, function(r) {
if (res.id == r.id) { found = true; };
if (md.id == r.id) { found = true; };
});
if (found) {
@ -58,17 +58,19 @@ app.controller('resourcesController', function($scope, $http) {
}
$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
$http.post('./api/resources/?' + $.now(), res).then(function successCallback(res) {
$scope.resources = res.data;
$http.post('./api/resources/?' + $.now(), md).then(function successCallback(res) {
alert("Resource saved");
$scope.reload();
}, function errorCallback(res) {
alert('ERROR: ' + res.data.message);
});
};
$scope.saveContent = function(id, content) {
$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
$http.post('./api/resources/' + id + '/content?' + $.now(), res).then(function successCallback(res) {
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
$http.post('./api/resources/' + id + '/content?' + $.now(), $.param({
'content' : content
})).then(function successCallback(res) {
alert("Resource saved");
}, function errorCallback(res) {
alert('ERROR: ' + res.data.message);