package com.adaptrex.tools.framework.javascript;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.apache.commons.lang3.StringUtils;
import com.adaptrex.tools.Tools;
import com.adaptrex.tools.environment.Environment;
import com.adaptrex.tools.environment.IEnvironmentService;
import com.adaptrex.tools.log.ILogService;
import com.adaptrex.tools.log.Log;
import com.google.inject.Inject;
public class DownloadService implements IDownloadService {
IEnvironmentService environmentService;
ILogService logService;
private Map<String,String> urlMap;
private String currentEntry = "";
@Inject
public DownloadService(IEnvironmentService environmentService, ILogService logService) {
this.environmentService = environmentService;
this.logService = logService;
this.urlMap = new HashMap<String,String>();
this.urlMap.put("adaptrex-js", "http://adaptrex.com/static/download/adaptrex-js.zip");
this.urlMap.put("extjs", "http://cdn.sencha.com/ext-4.1.1a-gpl.zip");
this.urlMap.put("sencha-touch", "http://cdn.sencha.io/touch/sencha-touch-2.1.0-gpl.zip");
}
@Override
public boolean downloadAndInstall(String framework) throws IOException {
Environment env = environmentService.getEnvironment();
String weblibPath = env.getWebrootPath() + "/weblib/";
String firstEntry = null;
String downloadPath = this.urlMap.get(framework);
URL url = new URL(downloadPath);
URLConnection conn = url.openConnection();
ZipInputStream zip = new ZipInputStream(conn.getInputStream());
ZipEntry entry;
while ((entry = zip.getNextEntry()) != null) {
String entryName = entry.getName();
if (firstEntry == null) {
/*
* If the top level of the extracted folder doesn't match
* the sdk type... add our own based on the zip file name
*/
File newDir = null;
if (!entryName.startsWith("adaptrex") &&
!entryName.startsWith("ext") &&
!entryName.startsWith("sencha")
) {
String manualFolderName = StringUtils.substringBefore(
StringUtils.substringAfterLast(downloadPath, "/"),
".zip");
weblibPath = weblibPath + manualFolderName + "/";
}
newDir = new File(weblibPath + entryName);
if (newDir.exists()) {
logService.log(Log.ERROR, "Framework already installed",
"The most recent version of " + framework + " is already installed" + Tools.CR +
"at " + newDir.getAbsolutePath()
);
zip.close();
return false;
}
newDir.mkdirs();
firstEntry = entryName;
this.currentEntry = entryName;
continue;
}
this.currentEntry = entryName;
if (entry.isDirectory()) {
File newDir = new File(weblibPath + entry.getName());
newDir.mkdir();
continue;
}
int size;
byte[] buffer = new byte[2048];
FileOutputStream fos = new FileOutputStream(weblibPath + entry.getName());
BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);
while ((size = zip.read(buffer, 0, buffer.length)) != -1) {
bos.write(buffer, 0, size);
}
bos.flush();
bos.close();
}
zip.close();
logService.log(Log.SUCCESS, framework + " downloaded and installed");
return true;
}
@Override
public String getCurrentEntry() {
return this.currentEntry;
}
}