use of ubic.gemma.model.common.description.LocalFile in project Gemma by PavlidisLab.
the class AbstractFetcher method getExistingFile.
/**
* Wrap the existing file in the required Collection<LocalFile>
*
* @param existingFile existing file
* @param seekFile seek file
* @return collection of local files
*/
protected Collection<LocalFile> getExistingFile(File existingFile, String seekFile) {
Collection<LocalFile> fallback = new HashSet<>();
LocalFile lf = LocalFile.Factory.newInstance();
try {
lf.setLocalURL(existingFile.toURI().toURL());
lf.setRemoteURL((new File(seekFile)).toURI().toURL());
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
lf.setSize(existingFile.length());
fallback.add(lf);
return fallback;
}
use of ubic.gemma.model.common.description.LocalFile in project Gemma by PavlidisLab.
the class FtpArchiveFetcher method doTask.
@Override
protected Collection<LocalFile> doTask(FutureTask<Boolean> future, long expectedSize, String seekFileName, String outputFileName) {
Executors.newSingleThreadExecutor().execute(future);
try {
File outputFile = new File(outputFileName);
boolean ok = this.waitForDownload(future, expectedSize, outputFile);
if (!ok) {
// probably cancelled.
return null;
} else if (future.get()) {
AbstractFetcher.log.info("Unpacking " + outputFile);
this.unPack(outputFile);
this.cleanUp(outputFile);
if (outputFile.isDirectory())
return this.listFiles(seekFileName, outputFile, null);
return this.listFiles(seekFileName, outputFile.getParentFile(), null);
}
} catch (ExecutionException e) {
future.cancel(true);
throw new RuntimeException("Couldn't fetch " + seekFileName + " from " + this.getNetDataSourceUtil().getHost(), e);
} catch (InterruptedException e) {
future.cancel(true);
throw new RuntimeException("Interrupted: Couldn't fetch " + seekFileName + " from " + this.getNetDataSourceUtil().getHost(), e);
} catch (IOException e) {
future.cancel(true);
throw new RuntimeException("IOException: Couldn't fetch " + seekFileName + " from " + this.getNetDataSourceUtil().getHost(), e);
}
future.cancel(true);
throw new RuntimeException("Couldn't fetch " + seekFileName + " from " + this.getNetDataSourceUtil().getHost());
}
use of ubic.gemma.model.common.description.LocalFile in project Gemma by PavlidisLab.
the class HttpFetcher method fetch.
public Collection<LocalFile> fetch(String url, String outputFileName) {
AbstractFetcher.log.info("Seeking " + url);
this.localBasePath = Settings.getDownloadPath();
try {
String host = (new URL(url)).getHost();
String filePath;
if (StringUtils.isBlank(host)) {
throw new IllegalArgumentException(url + " was not parsed into a valid URL");
}
filePath = url.replace(host, "");
filePath = filePath.replace("http://", "");
filePath = filePath.replace('?', '_');
filePath = filePath.replace('=', '_');
filePath = filePath.replace('&', '_');
filePath = filePath.replace('/', '_');
filePath = filePath.replaceFirst("^_", "");
File newDir = this.mkdir(host);
final String output;
if (outputFileName == null) {
output = this.formLocalFilePath(filePath, newDir);
} else {
output = outputFileName;
}
FutureTask<Boolean> future = this.defineTask(output, url);
return this.doTask(future, url, output);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of ubic.gemma.model.common.description.LocalFile in project Gemma by PavlidisLab.
the class LocalFileServiceImpl method findByPath.
@Override
public LocalFile findByPath(String path) {
File f = new File(path);
LocalFile seek = LocalFile.Factory.newInstance();
try {
seek.setLocalURL(f.toURI().toURL());
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
return this.localFileDao.find(seek);
}
use of ubic.gemma.model.common.description.LocalFile in project Gemma by PavlidisLab.
the class LocalFileServiceImpl method copyFile.
/**
* @see LocalFileService#copyFile(LocalFile, LocalFile)
*/
@Override
public LocalFile copyFile(LocalFile sourceFile, LocalFile targetFile) throws IOException {
File src = sourceFile.asFile();
if (src == null)
throw new IOException("Could not convert LocalFile into java.io.File");
File dst = targetFile.asFile();
if (dst == null)
throw new IOException("Could not convert LocalFile into java.io.File");
try (InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst)) {
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
long size = 0;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
size += len;
}
in.close();
out.close();
targetFile.setSize(size);
this.localFileDao.create(targetFile);
return targetFile;
}
}
Aggregations