Skip to content

Commit

Permalink
media file check in transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
surabhi-mahawar committed Jul 21, 2022
1 parent 99100aa commit 4d84d2e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public ServiceResponse greeting(@RequestParam(value = "previousPath", required =
log.info("InstanceCurrentXML" + instanceXMlPrevious);
log.info("botFormName" + botFormName);
String formPath = ODKConsumerReactive.getFormPath(botFormName);
ServiceResponse serviceResponse = new MenuManager(previousPath, currentAnswer, instanceXMlPrevious, formPath, botFormName, redisCacheService, "1234567890", "bot", null).start();
ServiceResponse serviceResponse = new MenuManager(previousPath, currentAnswer, instanceXMlPrevious, formPath, botFormName, redisCacheService, "1234567890", "bot", null, null).start();
System.out.println(serviceResponse.getCurrentResponseState());
return serviceResponse;
}
Expand Down
51 changes: 47 additions & 4 deletions src/main/java/com/uci/transformer/odk/MenuManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.uci.transformer.odk.utilities.Item;
import com.uci.utils.cache.service.RedisCacheService;

import com.uci.utils.cdn.FileCdnProvider;
import io.r2dbc.postgresql.codec.Json;
import lombok.*;
import lombok.extern.java.Log;
Expand Down Expand Up @@ -93,8 +94,12 @@ public class MenuManager {
RedisCacheService redisCacheService;
String userID;
String appID;
FileCdnProvider fileCdnProvider;

public MenuManager(String xpath, String answer, String instanceXML, String formPath, String formID, RedisCacheService redisCacheService, String userID, String appID, XMessagePayload payload) {
public MenuManager(String xpath, String answer, String instanceXML, String formPath,
String formID, RedisCacheService redisCacheService,
String userID, String appID, XMessagePayload payload,
FileCdnProvider fileCdnProvider) {
this.xpath = xpath;
this.answer = answer;
this.instanceXML = instanceXML;
Expand All @@ -106,6 +111,7 @@ public MenuManager(String xpath, String answer, String instanceXML, String formP
this.userID = userID;
this.appID = appID;
this.payload = payload;
this.fileCdnProvider = fileCdnProvider;

setAssesmentCharacters();
}
Expand Down Expand Up @@ -148,7 +154,8 @@ public MenuManager(String xpath, String answer, String instanceXML, String formP

public MenuManager(String xpath, String answer, String instanceXML, String formPath, String formID,
Boolean isPrefilled, QuestionRepository questionRepo, JSONObject user,
boolean shouldUpdateFormXML, RedisCacheService redisCacheService, XMessage xMsg) {
boolean shouldUpdateFormXML, RedisCacheService redisCacheService, XMessage xMsg,
FileCdnProvider fileCdnProvider) {
this.xpath = xpath;
this.answer = answer;
this.instanceXML = instanceXML;
Expand All @@ -163,6 +170,7 @@ public MenuManager(String xpath, String answer, String instanceXML, String formP
this.userID = xMsg.getTo().getUserID();
this.appID = xMsg.getApp();
this.payload = xMsg.getPayload();
this.fileCdnProvider = fileCdnProvider;

setAssesmentCharacters();
}
Expand Down Expand Up @@ -1227,7 +1235,8 @@ private XMessagePayload getPayloadWithBindTags(XMessagePayload payload, List<Tre
String tagText = attribute.getAttributeValue().toString();
StylingTag tag = StylingTag.getEnumByText(tagText);
if(tag != null ) {
if(CommonUtils.isStylingTagPublicMediaType(tag) || CommonUtils.isStylingTagCdnMediaType(tag)) {
/* If styling tag is media type & correspond to a valid media category */
if(CommonUtils.isStylingTagMediaType(tag) && CommonUtils.getMediaCategoryFromStylingTag(tag) != null) {
MessageMedia media = payload.getMedia();
if(media == null) {
media = new MessageMedia();
Expand Down Expand Up @@ -1255,9 +1264,43 @@ private XMessagePayload getPayloadWithBindTags(XMessagePayload payload, List<Tre
}
media.setText(attribute.getAttributeValue());
payload.setMedia(media);
// payload.setMediaCaption(attribute.getAttributeValue());
}
});

/* Media cdn url or public url find, else set text only */
if(payload.getMedia() != null && payload.getMedia().getUrl() != null
&& !payload.getMedia().getUrl().isEmpty()
&& payload.getMedia().getCategory() != null) {
MessageMedia media = payload.getMedia();
String mediaUrl = media.getUrl().replace("\n", "").replace("<br>", "").trim();
if (CommonUtils.isTextAUrl(mediaUrl)) {
mediaUrl = mediaUrl.trim();
} else if (fileCdnProvider != null) {
mediaUrl = fileCdnProvider.getFileSignedUrl(mediaUrl);
if(!CommonUtils.isTextAUrl(mediaUrl)) {
mediaUrl = "";
}
} else {
mediaUrl = "";
}

if(mediaUrl != null && !mediaUrl.isEmpty()) {
media.setUrl(mediaUrl);
if (media.getCategory().equals(MediaCategory.IMAGE) || media.getCategory().equals(MediaCategory.FILE)) {
/* Default caption */
if (media.getText() == null || media.getText().isEmpty()) {
media.setText(media.getCategory().toString());
}
}
payload.setMedia(media);
} else {
payload.setText(media.getUrl().trim());
payload.setMedia(null);
}
} else {
payload.setMedia(null);
}

} catch (Exception e) {
log.info("Exception in getPayloadWithBindTags: "+e.getMessage());
}
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/uci/transformer/odk/ODKConsumerReactive.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.uci.dao.models.XMessageDAO;
import com.uci.dao.repository.XMessageRepository;
import com.uci.transformer.TransformerProvider;
import com.uci.utils.cdn.FileCdnFactory;
import com.uci.utils.cdn.FileCdnProvider;
import com.uci.utils.service.UserService;
import com.uci.transformer.odk.entity.Assessment;
import com.uci.transformer.odk.entity.GupshupMessageEntity;
Expand Down Expand Up @@ -152,6 +154,9 @@ public class ODKConsumerReactive extends TransformerProvider {
@Autowired
public RedisCacheService redisCacheService;

@Autowired
public FileCdnFactory fileCdnFactory;

@EventListener(ApplicationStartedEvent.class)
public void onMessage() {
reactiveKafkaReceiver
Expand Down Expand Up @@ -276,7 +281,7 @@ public Mono<Mono<XMessage>> apply(FormManagerParams previousMeta) {
log.info("Condition 1 - xpath: null, answer: null, instanceXMlPrevious: "
+instanceXMlPrevious+", formPath: "+formPath+", formID: "+formID);
mm = new MenuManager(null, null, instanceXMlPrevious,
formPath, formID, redisCacheService, xMessage.getTo().getUserID(), xMessage.getApp(), xMessage.getPayload());
formPath, formID, redisCacheService, xMessage.getTo().getUserID(), xMessage.getApp(), xMessage.getPayload(), fileCdnFactory.getFileCdnProvider());
response[0] = mm.start();
} else {
FormInstanceUpdation ss = FormInstanceUpdation.builder().build();
Expand Down Expand Up @@ -309,7 +314,7 @@ public Mono<Mono<XMessage>> apply(FormManagerParams previousMeta) {
+", questionRepo: "+questionRepo+", user: "+user+", shouldUpdateFormXML: true, campaign: "+camp);
mm = new MenuManager(previousMeta.previousPath, answer,
instanceXMlPrevious, formPath, formID,
prefilled, questionRepo, user, true, redisCacheService, xMessage);
prefilled, questionRepo, user, true, redisCacheService, xMessage, fileCdnFactory.getFileCdnProvider());
}else{
prefilled = false;
answer = previousMeta.currentAnswer;
Expand All @@ -319,7 +324,7 @@ public Mono<Mono<XMessage>> apply(FormManagerParams previousMeta) {
+", questionRepo: "+questionRepo+", user: "+user+", shouldUpdateFormXML: true, campaign: "+camp);
mm = new MenuManager(previousMeta.previousPath, answer,
instanceXMlPrevious, formPath, formID,
prefilled, questionRepo, user, true, redisCacheService, xMessage);
prefilled, questionRepo, user, true, redisCacheService, xMessage, fileCdnFactory.getFileCdnProvider());
}
response[0] = mm.start();
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,12 @@ cdn.minio.fa.key=${CDN_MINIO_FA_KEY:#{"https://fa.example.io/"}}
cdn.minio.fa.url=${CDN_MINIO_FA_URL:#{"fakey"}}

#Azure Storage
spring.azure.blob.store.account.name=${AZURE_BLOB_STORE_ACCOUNT_KEY:#{""}}
spring.azure.blob.store.account.key=${AZURE_BLOB_STORE_ACCOUNT_NAME:#{""}}
spring.azure.blob.store.container.name=${AZURE_BLOB_STORE_CONTAINER:#{""}}
spring.azure.blob.store.account.name=${AZURE_BLOB_STORE_ACCOUNT_KEY}
spring.azure.blob.store.account.key=${AZURE_BLOB_STORE_ACCOUNT_NAME}
spring.azure.blob.store.container.name=${AZURE_BLOB_STORE_CONTAINER}

#Selected media cdn


#Telemetry Event
exhaust.telemetry.enabled=${EXHAUST_TELEMETRY_ENABLED}
Expand Down

0 comments on commit 4d84d2e

Please sign in to comment.