use of com.genexus.util.GXFile in project JavaClasses by genexuslabs.
the class GXPreparedStatement method setGXDbFileURI.
public void setGXDbFileURI(int index, String fileName, String blobPath, int length, String tableName, String fieldName) throws SQLException {
ExternalProvider storageProvider = Application.getExternalProvider();
fileName = fileName.trim();
blobPath = blobPath.trim();
String uploadNameValue = SpecificImplementation.GXutil.getUploadNameValue(blobPath);
// EMPTY BLOB
if (blobPath == null || blobPath.trim().length() == 0) {
setVarchar(index, ExternalProviderCommon.getProviderObjectAbsoluteUriSafe(storageProvider, fileName), length, false);
return;
}
String fileUri = "";
String multimediaTemporalPath = com.genexus.Preferences.getDefaultPreferences().getMultimediaPath();
if (fileName.length() > 0 && !(blobPath.startsWith(multimediaTemporalPath) && GXutil.isAbsoluteURL(fileName))) {
fileUri = GXDbFile.generateUri(fileName, !GXDbFile.hasToken(fileName), true);
} else if (blobPath.trim().length() > 0) {
blobPath = com.genexus.GXutil.cutUploadPrefix(blobPath);
File file = new File(blobPath);
fileUri = GXDbFile.generateUri(uploadNameValue.isEmpty() ? file.getName() : uploadNameValue, !GXDbFile.hasToken(blobPath), true);
}
boolean externalStorageEnabled = storageProvider != null;
// Should improve this condition in order to not depend on tableName and FieldName.
boolean attInExternalStorage = externalStorageEnabled && (tableName != null && fieldName != null);
if (!attInExternalStorage) {
setVarchar(index, fileUri, length, false);
return;
}
// External Storage is ENABLED. We have the following cases:
// - 1. WebUpload: The URL is an External Storage URL in the Private Temp Storage Folder.
// - 2. External URL: An absolute URL (outside External Storage).
// - 3. Transaction Update Mode (nothing changed): An Storage URL that is already in External Storage (except in private), should not be uploaded nor copied.
// - 4. Upload Resource from local drive to External Storage: Ex: Image.FromImage(ActionDelete)
String storageObjectName = ExternalProviderCommon.getProviderObjectName(storageProvider, blobPath);
boolean resourceAlreadyOnStorage = storageObjectName != null;
ResourceAccessControlList defaultAcl = ResourceAccessControlList.Default;
String storageTargetObjectName = blobPath;
boolean isPrivateTempUpload = storageTargetObjectName.startsWith(Application.getClientPreferences().getTMPMEDIA_DIR());
String folder = Application.getGXServices().get(GXServices.STORAGE_SERVICE).getProperties().get("FOLDER_NAME");
if (!resourceAlreadyOnStorage && (fileName.equals(blobPath) || GXutil.isAbsoluteURL(fileName))) {
// - 2. External URL: An absolute URL (outside External Storage).
try (InputStream is = new URL(fileName).openStream()) {
int idx = storageTargetObjectName.lastIndexOf("/");
if ((idx != -1) && (idx < storageTargetObjectName.length() - 1)) {
storageTargetObjectName = storageTargetObjectName.substring(idx + 1);
}
storageTargetObjectName = com.genexus.PrivateUtilities.getTempFileName("", CommonUtil.getFileName(storageTargetObjectName), CommonUtil.getFileType(storageTargetObjectName), true);
storageTargetObjectName = folder + "/" + tableName + "/" + fieldName + "/" + storageTargetObjectName;
fileUri = Application.getExternalProvider().upload(storageTargetObjectName, is, defaultAcl);
} catch (IOException e) {
throw new SQLException("An error occurred while downloading data from url: " + fileName + e.getMessage());
}
} else {
if (resourceAlreadyOnStorage && !isPrivateTempUpload) {
// - 3. Transaction Update Mode (nothing changed): An Storage URL that is already in External Storage (except in private), should not be uploaded nor copied.
fileUri = storageObjectName;
} else {
// Every temporal file is saved as private.
GXFile gxFile = new GXFile(storageTargetObjectName, ResourceAccessControlList.Private);
if (gxFile.exists()) {
// - 1. WebUpload: The URL is an External Storage URL in the Private Temp Storage Folder.
fileName = uploadNameValue.isEmpty() ? gxFile.getName() : uploadNameValue;
int idx = fileName.lastIndexOf("/");
if ((idx != -1) && (idx < fileName.length() - 1)) {
fileName = fileName.substring(idx + 1);
}
fileName = com.genexus.PrivateUtilities.getTempFileName("", CommonUtil.getFileName(fileName), CommonUtil.getFileType(fileName), true);
fileUri = storageProvider.copy(gxFile.getAbsoluteName(), fileName, tableName, fieldName, defaultAcl);
} else {
// - 4. Upload Resource from local drive to External Storage: Ex: Image.FromImage(ActionDelete)
int idx = blobPath.lastIndexOf("/");
if ((idx != -1) && (idx < blobPath.length() - 1)) {
fileName = blobPath.substring(idx + 1);
}
fileName = com.genexus.PrivateUtilities.getTempFileName("", CommonUtil.getFileName(fileName), CommonUtil.getFileType(fileName), true);
fileName = folder + "/" + tableName + "/" + fieldName + "/" + fileName;
if (con.getContext() != null) {
com.genexus.internet.HttpContext webContext = (HttpContext) con.getContext().getHttpContext();
if ((webContext != null) && (webContext instanceof com.genexus.webpanels.HttpContextWeb) && (blobPath.startsWith(webContext.getContextPath()) || blobPath.startsWith(webContext.getDefaultPath()))) {
blobPath = ((com.genexus.webpanels.HttpContextWeb) webContext).getRealPath(blobPath);
}
}
fileUri = storageProvider.upload(blobPath, fileName, defaultAcl);
}
}
}
setVarchar(index, fileUri, length, false);
}
use of com.genexus.util.GXFile in project JavaClasses by genexuslabs.
the class GXPreparedStatement method setBLOBFile.
public void setBLOBFile(int index, String fileName, boolean isMultiMedia) throws SQLException {
if (isMultiMedia && Application.getGXServices().get(GXServices.STORAGE_SERVICE) != null) {
fileName = "";
}
if (fileName != null && !fileName.trim().equals("") && !fileName.toLowerCase().trim().endsWith("about:blank") && Application.getGXServices().get(GXServices.STORAGE_SERVICE) == null) {
fileName = com.genexus.GXutil.cutUploadPrefix(fileName);
try {
if (fileName.toLowerCase().startsWith("http://") || fileName.toLowerCase().startsWith("https://")) {
int queryIndex = fileName.lastIndexOf('?');
if (queryIndex > -1)
fileName = fileName.substring(0, queryIndex + 1) + PrivateUtilities.encodeURL(fileName.substring(queryIndex + 1));
URL fileURL = new URL(fileName);
String blobPath = com.genexus.Preferences.getDefaultPreferences().getBLOB_PATH();
fileName = com.genexus.PrivateUtilities.getTempFileName(blobPath, CommonUtil.getFileName(fileName), CommonUtil.getFileType(fileName), true);
com.genexus.PrivateUtilities.InputStreamToFile(fileURL.openStream(), fileName);
}
} catch (MalformedURLException e) {
throw new SQLException("Malformed URL " + fileName);
} catch (IOException e) {
throw new SQLException("An error occurred while downloading data from url: " + fileName + e.getMessage());
}
if (con.getContext() != null) {
com.genexus.internet.HttpContext webContext = (HttpContext) con.getContext().getHttpContext();
if (webContext != null) {
if (webContext instanceof com.genexus.webpanels.HttpContextWeb) {
fileName = ((com.genexus.webpanels.HttpContextWeb) webContext).getRealPath(fileName);
} else {
if (!webContext.getDefaultPath().isEmpty() && !new File(fileName).isAbsolute()) {
if (fileName.startsWith(webContext.getContextPath())) {
fileName = fileName.substring(webContext.getContextPath().length() + 1);
}
fileName = webContext.getDefaultPath() + File.separator + fileName;
}
}
}
}
}
if (skipSetBlobs) {
if (blobFiles == null) {
blobFiles = new String[index];
} else if (blobFiles.length < index) {
String[] temp = blobFiles;
blobFiles = new String[index];
System.arraycopy(temp, 0, blobFiles, 0, temp.length);
}
blobFiles[index - 1] = fileName;
} else {
if (fileName != null && !fileName.trim().equals("") && !fileName.toLowerCase().trim().endsWith("about:blank")) {
if (Application.getExternalProvider() == null) {
try {
File file = new File(fileName);
BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file));
setBinaryStream(index, inputStream, (int) file.length());
} catch (IOException e) {
throw new SQLException("The filename does not exists in url " + fileName);
}
} else {
GXFile gxFile = new GXFile(fileName, ResourceAccessControlList.Private);
if (gxFile.exists()) {
InputStream is = gxFile.getStream();
setBinaryStream(index, is, (int) gxFile.getLength());
} else {
try {
File localFile = new File(fileName);
BufferedInputStream localInputStream = new BufferedInputStream(new FileInputStream(localFile));
setBinaryStream(index, localInputStream, (int) localFile.length());
} catch (IOException e) {
throw new SQLException("The filename does not exists in url " + fileName);
}
}
}
} else {
if (con.getDBMS().getId() == GXDBMS.DBMS_ORACLE) {
BufferedInputStream inputStream = new BufferedInputStream(new ByteArrayInputStream(new byte[] { 0 }));
setBinaryStream(index, inputStream, (int) 1);
} else {
BufferedInputStream inputStream = new BufferedInputStream(new ByteArrayInputStream(new byte[] {}));
setBinaryStream(index, inputStream, (int) 0);
}
}
}
}
use of com.genexus.util.GXFile in project JavaClasses by genexuslabs.
the class GXResultSet method getBLOBFile.
private String getBLOBFile(int columnIndex, String extension, String name, String fileName, boolean temporary) throws SQLException {
try {
InputStream source = getBinaryStream(columnIndex);
byte[] xbuffer = new byte[1];
int firstByte = 0;
int secondByte = 0;
if (source != null) {
firstByte = source.read(xbuffer);
if (con.getDBMS() instanceof GXDBMSoracle7) {
secondByte = source.read();
}
}
boolean isEmptyBuffer = false;
if ((source != null) && (source.available() == 0) && (firstByte != -1) && (xbuffer[0] == 0)) {
// Si en el InputStream no queda mas por leer y el unico byte leido es un 0
// se retorna el string vacio.
isEmptyBuffer = true;
}
if (result.wasNull() || source == null || firstByte == -1 || (con.getDBMS() instanceof GXDBMSoracle7 && secondByte == -1) || isEmptyBuffer) {
if (source != null) {
source.close();
}
return "";
}
if (Application.getGXServices().get(GXServices.STORAGE_SERVICE) == null) {
File path = new File(fileName).getParentFile();
if (!path.exists())
path.mkdirs();
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
OutputStream destination = new BufferedOutputStream(os);
byte[] buffer = new byte[4096];
int bytes_read;
destination.write(xbuffer, 0, 1);
if (con.getDBMS() instanceof GXDBMSoracle7) {
destination.write(secondByte);
}
while (true) {
bytes_read = source.read(buffer);
if (bytes_read == -1)
break;
destination.write(buffer, 0, bytes_read);
}
source.close();
destination.close();
InputStream is = new ByteArrayInputStream(os.toByteArray());
GXFile gxFile = new GXFile(fileName, ResourceAccessControlList.Private);
gxFile.create(is);
fileName = gxFile.getFilePath();
if (temporary) {
com.genexus.webpanels.BlobsCleaner.getInstance().addBlobFile(fileName);
}
} catch (IOException e) {
throw new SQLException("Can't read BLOB field into " + fileName);
}
return fileName;
}
use of com.genexus.util.GXFile in project JavaClasses by genexuslabs.
the class GXResultSet method getMultimediaUri.
public String getMultimediaUri(int columnIndex, boolean absPath) throws SQLException {
ExternalProvider provider = Application.getExternalProvider();
String colValue = getVarchar(columnIndex);
if (provider != null && colValue.length() > 0 && GXutil.isAbsoluteURL(colValue)) {
String providerObjectName = ExternalProviderCommon.getProviderObjectName(provider, colValue);
if (providerObjectName != null) {
return new GXFile(providerObjectName).getAbsolutePath();
}
}
return GXDbFile.resolveUri(colValue, absPath);
}
use of com.genexus.util.GXFile in project JavaClasses by genexuslabs.
the class ExcelSpreadsheet method saveAsImpl.
private Boolean saveAsImpl(String fileName) throws ExcelException {
ByteArrayOutputStream fs = null;
ByteArrayInputStream in = null;
GXFile file = null;
boolean savedOK = false;
autoFitColumns();
recalculateFormulas();
try {
fs = new ByteArrayOutputStream();
_workbook.write(fs);
in = new ByteArrayInputStream(fs.toByteArray());
fs.close();
file = new GXFile(fileName, Constants.EXTERNAL_PRIVATE_UPLOAD);
savedOK = file.create(in, true);
in.close();
file.close();
} catch (Exception e) {
try {
if (fs != null)
fs.close();
if (in != null)
in.close();
if (file != null)
file.close();
} catch (Exception e1) {
logger.error("saveAsImpl", e1);
}
throw new ExcelException(12, "GeneXus Office Module Error: " + e.toString());
}
return savedOK;
}
Aggregations