package com.psddev.dari.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/** Item stored in the local file system. */
public class LocalStorageItem extends AbstractStorageItem {

    /** Setting key for root path. */
    public static final String ROOT_PATH_SETTING = "rootPath";

    private transient String rootPath;

    /** Returns the root path. */
    public String getRootPath() {
        return rootPath;
    }

    /** Sets the root path. */
    public void setRootPath(String rootPath) {
        this.rootPath = rootPath;
    }

    // --- AbstractStorageItem support ---

    @Override
    public void initialize(String settingsKey, Map<String, Object> settings) {
        super.initialize(settingsKey, settings);

        setRootPath(ObjectUtils.to(String.class, settings.get(ROOT_PATH_SETTING)));
        if (ObjectUtils.isBlank(getRootPath())) {
            throw new SettingsException(settingsKey + "/" + ROOT_PATH_SETTING, "No root path!");
        }
    }

    @Override
    protected InputStream createData() throws IOException {
        return new FileInputStream(new File(getRootPath() + "/" + getPath()));
    }

    @Override
    protected void saveData(InputStream data) throws IOException {
        File file = new File(getRootPath() + "/" + getPath());
        IoUtils.createParentDirectories(file);
        FileOutputStream output = null;

        try {
            output = new FileOutputStream(file);
            int bytesRead;
            byte[] buffer = new byte[4096];
            while ((bytesRead = data.read(buffer)) > 0) {
                output.write(buffer, 0, bytesRead);
            }

        } finally {
            if (output != null) {
                output.close();
            }
        }
    }

    @Override
    public boolean isInStorage() {
        return new File(getRootPath() + "/" + getPath()).exists();
    }
}

@@ -292,16 +292,48 @@
 ageItem 
+implements StorageItemOriginUrl 
 %7B%0A%0A    /
@@ -437,41 +437,305 @@
     
-private transient String rootPath
+/**%0A     * Sub-setting key for the private base URL that's used to construct the%0A     * %7B@linkplain #getOriginUrl private URL%7D.%0A     */%0A    public static final String ORIGIN_BASE_URL_SUB_SETTING = %22originBaseUrl%22;%0A%0A    private transient String rootPath;%0A%0A    private transient String originBaseUrl
 ;%0A%0A 
@@ -1219,24 +1219,123 @@
 SETTING)));%0A
+        setOriginBaseUrl(ObjectUtils.to(String.class, settings.get(ORIGIN_BASE_URL_SUB_SETTING)));%0A
         if (
@@ -2375,11 +2375,405 @@
 );%0A    %7D
+%0A%0A    public void setOriginBaseUrl(String originBaseUrl) %7B%0A        this.originBaseUrl = originBaseUrl;%0A    %7D%0A%0A    public String getOriginBaseUrl() %7B%0A        return originBaseUrl;%0A    %7D%0A%0A    @Override%0A    public String getOriginUrl() %7B%0A        if (ObjectUtils.isBlank(getOriginBaseUrl())) %7B%0A            return null;%0A        %7D%0A%0A        return createPublicUrl(getOriginBaseUrl(), getPath());%0A    %7D
 %0A%7D%0A

Add originBaseUrl support to LocalStorageItem