use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.
the class FilesResource method onPost.
/**
* Handles the upload of a <em>single</em> file. Adds the given file to the current artifact template.
* <p>
* If the file already exists, is it <em>overridden</em>
*
* @return JSON with data required by JQuery-File-Upload (see https://github.com/blueimp/jQuery-File-Upload/wiki/Setup)
*/
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response onPost(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail, @FormDataParam("file") FormDataBodyPart body, @Context UriInfo uriInfo) {
// existence check not required as instantiation of the resource ensures that the object only exists if the resource exists
FilesResource.LOGGER.debug("Beginning with file upload");
String fileName = fileDetail.getFileName();
if (StringUtils.isEmpty(fileName)) {
return Response.status(Status.BAD_REQUEST).build();
}
RepositoryFileReference ref = this.fileName2fileRef(fileName, false);
// TODO: instead of fixing the media type, we could overwrite the browser's mediatype by using some user configuration
BufferedInputStream bis = new BufferedInputStream(uploadedInputStream);
org.apache.tika.mime.MediaType mediaType = BackendUtils.getFixedMimeType(bis, fileName, org.apache.tika.mime.MediaType.parse(body.getMediaType().toString()));
Response response = RestUtils.putContentToFile(ref, bis, mediaType);
if (response.getStatus() == Status.INTERNAL_SERVER_ERROR.getStatusCode()) {
return response;
}
try {
BackendUtils.synchronizeReferences((ArtifactTemplateId) fileDir.getParent());
} catch (IOException e) {
throw new WebApplicationException(e, Status.INTERNAL_SERVER_ERROR);
}
String URL = RestUtils.getAbsoluteURL(this.fileDir) + Util.URLencode(fileName);
return Response.created(RestUtils.createURI(URL)).entity(this.getAllFileMetas()).build();
}
Aggregations