Skip to content

Commit

Permalink
SEBSERV-553 fixed file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
anhefti committed Jun 25, 2024
1 parent abe8bcd commit 07bbf6c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ String callMoodleAPIFunction(

String uploadMultiPart(
String uploadEndpoint,
MultiValueMap<String, Object> multiPartAttributes);
MultiValueMap<String, Object> multiPartAttributes,
MultiValueMap<String, String> queryAttributes);


/** This maps a Moodle warning JSON object */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@

package ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle;

import javax.net.ssl.SSLContext;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -27,10 +24,6 @@
import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle.plugin.MoodlePluginCourseRestriction;
import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle.plugin.MoodlePluginFullIntegration;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.TrustStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
Expand Down Expand Up @@ -436,15 +429,27 @@ public String callMoodleAPIFunction(
@Override
public String uploadMultiPart(
final String uploadEndpoint,
final MultiValueMap<String, Object> multiPartAttributes) {
final MultiValueMap<String, Object> multiPartAttributes,
final MultiValueMap<String, String> queryAttributes) {

final LmsSetup lmsSetup = this.apiTemplateDataSupplier.getLmsSetup();
final String uri = lmsSetup.lmsApiUrl + uploadEndpoint;
final StringBuilder uri = new StringBuilder(lmsSetup.lmsApiUrl + uploadEndpoint);
getAccessToken();
multiPartAttributes.add("token", this.accessToken);
queryAttributes.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));
}
});

return super.postForObject(
uri,
uri.toString(),
multiPartAttributes,
String.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ public Result<Exam> applyConnectionConfiguration(final Exam exam, final byte[] c
multiPartAttributes.add("quizid", quizId);
multiPartAttributes.add("name", fileName);
multiPartAttributes.add("filename", fileName);

final MultiValueMap<String, String> queryAttributes = new LinkedMultiValueMap<>();
multiPartAttributes.add("quizid", quizId);
final ByteArrayResource contentsAsResource = new ByteArrayResource(configData) {
@Override
public String getFilename() {
Expand All @@ -260,7 +263,10 @@ public String getFilename() {
multiPartAttributes.add("file", contentsAsResource);

final MoodleAPIRestTemplate rest = getRestTemplate().getOrThrow();
final String response = rest.uploadMultiPart(UPLOAD_ENDPOINT, multiPartAttributes);
final String response = rest.uploadMultiPart(
UPLOAD_ENDPOINT,
multiPartAttributes,
queryAttributes);

if (response != null) {
log.info("Upload Connection Configuration to Moodle: quizid: {}, fileName: {} response: {}", quizId, fileName, response );
Expand Down

0 comments on commit 07bbf6c

Please sign in to comment.