fixed Moodle file upload
This commit is contained in:
parent
b33998be07
commit
644f0a4cd1
3 changed files with 66 additions and 40 deletions
|
@ -63,8 +63,9 @@ public interface MoodleAPIRestTemplate {
|
||||||
|
|
||||||
String uploadMultiPart(
|
String uploadMultiPart(
|
||||||
String uploadEndpoint,
|
String uploadEndpoint,
|
||||||
MultiValueMap<String, Object> multiPartAttributes,
|
String quizId,
|
||||||
MultiValueMap<String, String> queryAttributes);
|
String fileName,
|
||||||
|
byte[] configData);
|
||||||
|
|
||||||
|
|
||||||
/** This maps a Moodle warning JSON object */
|
/** This maps a Moodle warning JSON object */
|
||||||
|
|
|
@ -26,12 +26,7 @@ import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle.plugin.Mood
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.http.HttpEntity;
|
import org.springframework.http.*;
|
||||||
import org.springframework.http.HttpHeaders;
|
|
||||||
import org.springframework.http.HttpMethod;
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.MediaType;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
|
@ -429,31 +424,60 @@ public class MoodleRestTemplateFactoryImpl implements MoodleRestTemplateFactory
|
||||||
@Override
|
@Override
|
||||||
public String uploadMultiPart(
|
public String uploadMultiPart(
|
||||||
final String uploadEndpoint,
|
final String uploadEndpoint,
|
||||||
final MultiValueMap<String, Object> multiPartAttributes,
|
final String quizId,
|
||||||
final MultiValueMap<String, String> queryAttributes) {
|
final String fileName,
|
||||||
|
final byte[] configData) {
|
||||||
|
|
||||||
final LmsSetup lmsSetup = this.apiTemplateDataSupplier.getLmsSetup();
|
final LmsSetup lmsSetup = this.apiTemplateDataSupplier.getLmsSetup();
|
||||||
final StringBuilder uri = new StringBuilder(lmsSetup.lmsApiUrl + uploadEndpoint);
|
final StringBuilder uri = new StringBuilder(lmsSetup.lmsApiUrl + uploadEndpoint);
|
||||||
getAccessToken();
|
getAccessToken();
|
||||||
queryAttributes.add("token", this.accessToken.toString());
|
|
||||||
|
|
||||||
queryAttributes.forEach((key, values) -> {
|
final HttpHeaders headers = new HttpHeaders();
|
||||||
if (values.isEmpty()) {
|
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
||||||
return;
|
final MultiValueMap<String, String> fileMap = new LinkedMultiValueMap<>();
|
||||||
}
|
final ContentDisposition contentDisposition = ContentDisposition
|
||||||
if (uri.toString().contains("?")) {
|
.builder("form-data")
|
||||||
uri.append("&").append(key).append("=").append(values.get(0));
|
.name("file")
|
||||||
} else {
|
.filename(fileName)
|
||||||
uri.append("?").append(key).append("=").append(values.get(0));
|
.build();
|
||||||
}
|
fileMap.add(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString());
|
||||||
});
|
final HttpEntity<byte[]> fileEntity = new HttpEntity<>(configData, fileMap);
|
||||||
|
|
||||||
log.info("Upload to Moodle url: {}", uri.toString());
|
final MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
|
||||||
|
body.add("token", this.accessToken.toString());
|
||||||
|
body.add("quizid", quizId);
|
||||||
|
body.add("file", fileEntity);
|
||||||
|
|
||||||
return super.postForObject(
|
final HttpEntity<MultiValueMap<String, Object>> requestEntity =
|
||||||
|
new HttpEntity<>(body, headers);
|
||||||
|
|
||||||
|
final ResponseEntity<String> exchange = super.exchange(
|
||||||
uri.toString(),
|
uri.toString(),
|
||||||
multiPartAttributes,
|
HttpMethod.POST,
|
||||||
|
requestEntity,
|
||||||
String.class);
|
String.class);
|
||||||
|
|
||||||
|
return exchange.getBody();
|
||||||
|
|
||||||
|
// multiPartAttributes.add("token", this.accessToken.toString());
|
||||||
|
//
|
||||||
|
// queryAttributes.forEach((key, values) -> {
|
||||||
|
// if (values.isEmpty()) {
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// if (uri.toString().contains("?")) {
|
||||||
|
// uri.append("&").append(key).append("=").append(values.get(0));
|
||||||
|
// } else {
|
||||||
|
// uri.append("?").append(key).append("=").append(values.get(0));
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
//
|
||||||
|
// log.info("Upload to Moodle url: {}", uri.toString());
|
||||||
|
//
|
||||||
|
// return super.postForObject(
|
||||||
|
// uri.toString(),
|
||||||
|
// multiPartAttributes,
|
||||||
|
// String.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String doRequest(
|
private String doRequest(
|
||||||
|
|
|
@ -246,27 +246,28 @@ public class MoodlePluginFullIntegration implements FullLmsIntegrationAPI {
|
||||||
final String quizId = MoodleUtils.getQuizId(exam.externalId);
|
final String quizId = MoodleUtils.getQuizId(exam.externalId);
|
||||||
final String fileName = getConnectionConfigFileName(exam);
|
final String fileName = getConnectionConfigFileName(exam);
|
||||||
|
|
||||||
final MultiValueMap<String, Object> multiPartAttributes = new LinkedMultiValueMap<>();
|
// final MultiValueMap<String, Object> multiPartAttributes = new LinkedMultiValueMap<>();
|
||||||
multiPartAttributes.add("quizid", quizId);
|
// multiPartAttributes.add("quizid", quizId);
|
||||||
multiPartAttributes.add("name", fileName);
|
// multiPartAttributes.add("name", fileName);
|
||||||
multiPartAttributes.add("filename", fileName);
|
// multiPartAttributes.add("filename", fileName);
|
||||||
|
|
||||||
final MultiValueMap<String, String> queryAttributes = new LinkedMultiValueMap<>();
|
// final MultiValueMap<String, String> queryAttributes = new LinkedMultiValueMap<>();
|
||||||
queryAttributes.add("quizid", quizId);
|
// //queryAttributes.add("quizid", quizId);
|
||||||
final ByteArrayResource contentsAsResource = new ByteArrayResource(configData) {
|
// final ByteArrayResource contentsAsResource = new ByteArrayResource(configData) {
|
||||||
@Override
|
// @Override
|
||||||
public String getFilename() {
|
// public String getFilename() {
|
||||||
return fileName; // Filename has to be returned in order to be able to post.
|
// return fileName; // Filename has to be returned in order to be able to post.
|
||||||
}
|
// }
|
||||||
};
|
// };
|
||||||
|
//
|
||||||
multiPartAttributes.add("file", contentsAsResource);
|
// multiPartAttributes.add("file", contentsAsResource);
|
||||||
|
|
||||||
final MoodleAPIRestTemplate rest = getRestTemplate().getOrThrow();
|
final MoodleAPIRestTemplate rest = getRestTemplate().getOrThrow();
|
||||||
final String response = rest.uploadMultiPart(
|
final String response = rest.uploadMultiPart(
|
||||||
UPLOAD_ENDPOINT,
|
UPLOAD_ENDPOINT,
|
||||||
multiPartAttributes,
|
quizId,
|
||||||
queryAttributes);
|
fileName,
|
||||||
|
configData);
|
||||||
|
|
||||||
if (response != null) {
|
if (response != null) {
|
||||||
log.info("Upload Connection Configuration to Moodle: quizid: {}, fileName: {} response: {}", quizId, fileName, response );
|
log.info("Upload Connection Configuration to Moodle: quizid: {}, fileName: {} response: {}", quizId, fileName, response );
|
||||||
|
|
Loading…
Reference in a new issue