changed the business logic to show the preview on files

This commit is contained in:
Francesco Mangiacrapa 2020-07-22 17:35:33 +02:00
parent aec8ae3332
commit 2e6b3d031f
1 changed files with 105 additions and 87 deletions

View File

@ -62,7 +62,7 @@ import com.google.gwt.user.server.Base64Utils;
*/ */
public class DialogGetInfoBootstrap extends Composite { public class DialogGetInfoBootstrap extends Composite {
private static final int PREVIEW_WAITING_TIME = 10000; private static final int PREVIEW_WAITING_TIME = 9000; //9 sec
private static DialogGetInfoBootstrapUiBinder uiBinder = GWT.create(DialogGetInfoBootstrapUiBinder.class); private static DialogGetInfoBootstrapUiBinder uiBinder = GWT.create(DialogGetInfoBootstrapUiBinder.class);
@ -197,6 +197,10 @@ public class DialogGetInfoBootstrap extends Composite {
private Image noPreviewAvailable = new Image(Resources.getPreviewNotAvailable()); private Image noPreviewAvailable = new Image(Resources.getPreviewNotAvailable());
private Frame iFrameGDV = null;
private Timer timerGDV = null;
/** /**
* Instantiates a new dialog get info bootstrap. * Instantiates a new dialog get info bootstrap.
* *
@ -335,113 +339,127 @@ public class DialogGetInfoBootstrap extends Composite {
loadACLsDescriptionForSharedFolder(fileModel.getIdentifier()); loadACLsDescriptionForSharedFolder(fileModel.getIdentifier());
} }
boolean previewManaged = false;
if (typeEnum != null) { if (typeEnum != null) {
// is it an image? // is it an image?
if (typeEnum.equals(GXTFolderItemTypeEnum.IMAGE_DOCUMENT) if (typeEnum.equals(GXTFolderItemTypeEnum.IMAGE_DOCUMENT)
|| typeEnum.equals(GXTFolderItemTypeEnum.EXTERNAL_IMAGE)) { || typeEnum.equals(GXTFolderItemTypeEnum.EXTERNAL_IMAGE)) {
previewManaged = true;
loadThumbnailsForImage(); loadThumbnailsForImage();
} }
// is it a GCUBE-Item? // is it a GCUBE-Item?
if (typeEnum.equals(GXTFolderItemTypeEnum.GCUBE_ITEM)) { if (typeEnum.equals(GXTFolderItemTypeEnum.GCUBE_ITEM)) {
previewManaged = true; //preview not avaible for the type GCUBE_ITEM
loadGcubeItemProperties(); loadGcubeItemProperties();
} }
}
//If the preview is not managed
//through the previous code
//managing it by checking the mime-type
if(!previewManaged) {
// SOLUTION BASED ON GOOGLE DOC VIEWER if (!fileModel.isDirectory() && mapAllowedMimeTypesForPreview.containsKey(fileModel.getType())) {
if (typeEnum.equals(GXTFolderItemTypeEnum.EXTERNAL_PDF_FILE) // SOLUTION BASED ON GOOGLE DOC VIEWER
|| typeEnum.equals(GXTFolderItemTypeEnum.PDF_DOCUMENT) GWT.log("Mime type " + fileModel.getType() + " allowed for preview, try to display it");
|| typeEnum.equals(GXTFolderItemTypeEnum.EXTERNAL_FILE)
|| typeEnum.equals(GXTFolderItemTypeEnum.GCUBE_ITEM)
|| typeEnum.equals(GXTFolderItemTypeEnum.URL_DOCUMENT)
|| typeEnum.equals(GXTFolderItemTypeEnum.METADATA)){
if(mapAllowedMimeTypesForPreview.containsKey(fileModel.getType())){ final HTML loadingPreviewHTML = new HTML();
GWT.log("Mime type "+fileModel.getType()+" allowed for preview, try to display it"); setPlaceholder(loadingPreviewHTML, true, "loading preview...");
htmlPanelFilePreview.add(loadingPreviewHTML);
htmlPanelFilePreview.setVisible(true);
AppControllerExplorer.rpcWorkspaceService.getPublicLinkForFileItemId(fileModel.getIdentifier(), false, AppControllerExplorer.rpcWorkspaceService.getPublicLinkForFileItemId(fileModel.getIdentifier(), false,
new AsyncCallback<PublicLink>() { new AsyncCallback<PublicLink>() {
@Override @Override
public void onFailure(Throwable caught) { public void onFailure(Throwable caught) {
} removePlaceHolder(loadingPreviewHTML);
GWT.log("Error on loading the Public link for: "+fileModel.getIdentifier());
htmlPanelFilePreview.add(noPreviewAvailable);
htmlPanelFilePreview.setVisible(true);
}
@Override @Override
public void onSuccess(PublicLink result) { public void onSuccess(PublicLink result) {
GWT.log("The PublicLink link is: " + result); GWT.log("The PublicLink link is: " + result);
if (result != null) { if (result != null) {
//if file size is null or greater than 25MB // if file size is null or greater than 25MB
long byteTo25MB = 1024*1024*25; long byteTo25MB = 1024 * 1024 * 25;
GWT.log("The file size is: "+fileSize); GWT.log("The file size is: " + fileSize);
if(fileSize==null || fileSize>byteTo25MB) { if (fileSize == null || fileSize > byteTo25MB) {
GWT.log("The file size is null or greater than "+byteTo25MB+", returning"); GWT.log("The file size is null or greater than " + byteTo25MB + ", returning");
//htmlPanelFilePreview.add(new Image(Resources.getPreviewNotAvailable())); // htmlPanelFilePreview.add(new Image(Resources.getPreviewNotAvailable()));
return; return;
} }
// String pdfPreview = "<iframe frameBorder=\"0\" class=\"my-preview-doc\" " String googleDocViewerURL = "https://docs.google.com/viewer?url="
// + "src=\"https://docs.google.com/viewer?url=" + URL.encode(result.getCompleteURL()) + "&embedded=true";
// + URL.encode(result.getCompleteURL()) + "&embedded=true\">" + "</iframe>";
String googleDocViewerURL = "https://docs.google.com/viewer?url=" iFrameGDV = instanceFrame(googleDocViewerURL, loadingPreviewHTML);
+ URL.encode(result.getCompleteURL()) + "&embedded=true";
final HTML loadingPreviewHTML = new HTML(); final long startTime = new Date().getTime();
setPlaceholder(loadingPreviewHTML, true, "loading preview..."); timerGDV = new Timer() {
final Frame frame = instanceFrame(googleDocViewerURL, loadingPreviewHTML); @Override
public void run() {
final long startTime = new Date().getTime(); GWT.log("Checking if the iFrameGoogleDocViewer is ready");
Timer timer = new Timer() { if (iFrameGoogleDocViewerLoaded) {
removePlaceHolder(loadingPreviewHTML);
@Override GWT.log("iFrameGoogleDocViewer currently loaded, cancelling timer");
public void run() { cancel();
GWT.log("Checking if the iFrameGoogleDocViewer is ready"); return;
if(iFrameGoogleDocViewerLoaded) { }
removePlaceHolder(loadingPreviewHTML); long checkTime = new Date().getTime();
GWT.log("iFrameGoogleDocViewer currently loaded, cancelling timer"); long diff = checkTime - startTime;
if (diff > PREVIEW_WAITING_TIME) {// is greater than 10 sec
try {
GWT.log("iFrameGoogleDocViewer not loaded within "+PREVIEW_WAITING_TIME+" sec, cancelling timer, removing iframe");
cancel(); cancel();
return; removePlaceHolder(loadingPreviewHTML);
} htmlPanelFilePreview.add(noPreviewAvailable);
long checkTime = new Date().getTime(); iFrameGDV.setVisible(false);
long diff = checkTime - startTime; htmlPanelFilePreview.remove(iFrameGDV);
if(diff>PREVIEW_WAITING_TIME) {//is greater than 10 sec } catch (Exception e) {
try { // Silent
GWT.log("iFrameGoogleDocViewer not loaded within 5 sec, cancelling timer, removing iframe");
cancel();
removePlaceHolder(loadingPreviewHTML);
htmlPanelFilePreview.add(noPreviewAvailable);
frame.setVisible(false);
htmlPanelFilePreview.remove(frame);
}catch (Exception e) {
//Silent
}
} }
} }
}; }
timer.scheduleRepeating(200); };
timerGDV.scheduleRepeating(PREVIEW_WAITING_TIME/3);
htmlPanelFilePreview.add(iFrameGDV);
htmlPanelFilePreview.add(loadingPreviewHTML);
htmlPanelFilePreview.add(frame);
htmlPanelFilePreview.setVisible(true);
}
} }
}); }
}else { });
GWT.log("Mime type "+fileModel.getType()+" NOT allowed for preview, displaying 'No preview available'"); } else {
htmlPanelFilePreview.add(noPreviewAvailable); GWT.log("Mime type " + fileModel.getType() + " NOT allowed for preview, displaying 'No preview available'");
htmlPanelFilePreview.setVisible(true); htmlPanelFilePreview.add(noPreviewAvailable);
} htmlPanelFilePreview.setVisible(true);
} }
} }
addHandlers(); addHandlers();
} }
@Override
protected void onDetach() {
super.onDetach();
GWT.log("Detached...");
htmlPanelFilePreview.clear();
if(timerGDV!=null) {
try {
timerGDV.cancel();
}catch (Exception e) {
// TODO: handle exception
}
}
}
public Frame instanceFrame(String fileURL, final HTML thePreviewPlaceholder) { public Frame instanceFrame(String fileURL, final HTML thePreviewPlaceholder) {
//addLoading(); //addLoading();
String urlEncoded = URL.encode(fileURL); String urlEncoded = URL.encode(fileURL);