package helper;

import java.io.File;
import java.io.IOException;
import java.util.Base64;
import java.util.zip.CRC32;

import com.google.common.io.Files;


public class Storage {

    private static String storageLocation = "/tmp/thumby-storage";
    private long partitions =100;;

    public Storage(String loc) {
        if (loc != null) {
            storageLocation = loc;
        }
        play.Logger.debug("Store content in: " + storageLocation);
        for(int i=0;i<=partitions;i++){
            new File(storageLocation+File.separator+i).mkdirs(); 
        }
    }

    public File get(String key) {
        File target = findTarget(key);
        play.Logger.debug("SEARCH "+target);
        if (target.exists()) {
            return target;
        }
        return null;
    }

    public void set(String key, File result) {
        File target = findTarget(key);
        play.Logger.debug("CREATE "+target);
        try {
            Files.copy(result, target);
        } catch (IOException e) {
            play.Logger.debug("",e);
            throw new RuntimeException("", e);
        }
    } 

    private File findTarget(String key) {
        String name = encode(key);
        String dirname = getDirName(name);
        return new File(storageLocation + File.separator + dirname + File.separator + name);
    }

    private String getDirName(String name) {
        CRC32 crc = new CRC32();
        crc.update(name.getBytes());
        long num = crc.getValue();
        long mod = num % partitions;
        String dirname = "" + mod;
        return dirname;
    }

    private static String encode(String encodeMe) {
        return Base64.getEncoder().encodeToString(encodeMe.getBytes()).replaceAll("/", "-").replaceAll("\\+", "_");
    }

    private static String decode(String decodeMe) {
        String base64EncodedName = decodeMe.replaceAll("-", "/").replaceAll("_", "+");
        return new String(Base64.getDecoder().decode(base64EncodedName));
    }
}

@@ -1,8 +1,696 @@
+/*Copyright (c) 2018 %22hbz%22%0A%0AThis file is part of thumby.%0A%0Athumby is free software: you can redistribute it and/or modify%0Ait under the terms of the GNU Affero General Public License as%0Apublished by the Free Software Foundation, either version 3 of the%0ALicense, or (at your option) any later version.%0A%0AThis program is distributed in the hope that it will be useful,%0Abut WITHOUT ANY WARRANTY; without even the implied warranty of%0AMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the%0AGNU Affero General Public License for more details.%0A%0AYou should have received a copy of the GNU Affero General Public License%0Aalong with this program. If not, see %3Chttp://www.gnu.org/licenses/%3E.%0A */%0A
 package 
@@ -837,16 +837,54 @@
 Files;%0A%0A
+/**%0A * %0A * @author Jan Schnasse%0A *%0A */
 %0Apublic 

add license