package com.netfever.web.storage.datasources;
import java.io.ByteArrayOutputStream;
import java.io.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.netfever.common.datasource.api.IItem;
import com.netfever.common.datasource.api.IPath;
import com.netfever.common.datasource.api.ISystemRoot;
import com.netfever.common.utils.FileUtils;
import com.netfever.web.storage.api.IMimeFactory;
import com.netfever.web.storage.api.IWebDatasource;
import com.netfever.web.storage.api.IWebStream;
import com.netfever.web.storage.api.StorageException;
public final class LocalWebDatasource implements IWebDatasource {
private final Logger log = LoggerFactory.getLogger(LocalWebDatasource.class);
private File root;
private IMimeFactory mimeFactory;
private ISystemRoot systemRoot;
private void printFs(IPath path, String prefix) {
System.out.println(prefix + path.getName() + "\t(" + path.getAbsoluteName() + ")");
IItem[] items = path.enumerate();
for (IItem i: items) {
if (i instanceof IPath) {
printFs((IPath)i, prefix + "\t");
} else {
com.netfever.common.datasource.api.IFile f = (com.netfever.common.datasource.api.IFile)i;
System.out.println("\t" + prefix + f.getName() + "\t(" + f.getAbsoluteName() + ")");
}
}
}
private void printFs() {
IPath root = systemRoot.getRoot();
printFs(root, "");
}
public LocalWebDatasource(IMimeFactory mimeFactory, ISystemRoot systemRoot) {
super();
this.mimeFactory = mimeFactory;
this.root = null;
this.systemRoot = systemRoot;
printFs();
}
@Override
public boolean exists(String path) {
try {
getFile(path, true);
return true;
} catch (Exception e) {
return false;
}
}
@Override
public IWebStream openRead(String path) throws StorageException {
IWebStream res;
File f;
String mime;
f = getFile(path, true);
mime = getMimeFile(f);
if (mime == null) {
mime = this.mimeFactory.createMime(FileUtils.getFileExtension(f));
}
res = new LocalWebStream(mime, f);
return res;
}
@Override
public IWebStream openWrite(String path, String mime) throws StorageException {
return null;
}
public String getRoot() {
if (this.root == null) {
return null;
} else {
return this.root.getAbsolutePath();
}
}
public void setRoot(String root) throws StorageException {
this.root = new File(root);
if (!this.root.isDirectory()) {
if (!this.root.exists()) {
throw new StorageException("Setting root to {" + root + "} but folder does not exists");
} else {
throw new StorageException("Setting root to {" + root + "} - it exists but is not a folder");
}
} else {
log.info("Setting root to {" + root + "}");
}
}
protected File getFile(String path, boolean checkExists) throws StorageException {
File res;
if (this.root == null) {
throw new StorageException("The root has not been set so couldn't get a handle to {" + path + "}");
}
res = new File(this.root, path);
if (checkExists) {
if (!res.exists()) {
throw new StorageException("File {" + res.getAbsolutePath() + "} does not exists.");
}
if (!res.isFile()) {
throw new StorageException("File {" + res.getAbsolutePath() + "} exists but it's not a file.");
}
} else {
if (!res.isDirectory()) {
throw new StorageException("File {" + res.getAbsolutePath() + "} is a folder not a file.");
}
}
return res;
}
protected final String getMimeFile(File f) throws StorageException {
File fmime;
ByteArrayOutputStream out;
try {
fmime = new File(f.getAbsolutePath() + ".mime");
if (fmime.isFile()) {
log.info("Found mime for file {" + fmime.getAbsolutePath() + "}");
out = new ByteArrayOutputStream();
FileUtils.read(fmime, out);
return new String(out.toByteArray());
} else {
return null;
}
} catch (Exception e) {
throw new StorageException(e.getMessage(), e);
}
}
protected final void setMimeFile(File f, String mime) throws StorageException {
File fmime;
try {
fmime = new File(f.getAbsolutePath() + ".mime");
FileUtils.write(fmime, mime.getBytes());
} catch (Exception e) {
throw new StorageException(e.getMessage(), e);
}
}
public ISystemRoot getSystemRoot() {
return this.systemRoot;
}
public void setSystemRoot(ISystemRoot systemRoot) {
this.systemRoot = systemRoot;
}
}