package com.wizzardo.jrt;

import com.wizzardo.http.TokenizedFileTreeHandler;
import com.wizzardo.http.filter.TokenFilter;
import com.wizzardo.http.request.Header;
import com.wizzardo.http.request.Request;
import com.wizzardo.http.response.Response;

import java.io.*;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by wizzardo on 03/12/16.
 */
public class M3UHandler extends TokenizedFileTreeHandler {

    protected Set<String> extensions = new HashSet<>(Arrays.asList(
            "mkv",
            "avi",
            "mp4",
            "mp3",
            "aac",
            "flac",
            "ape",
            "wav",
            "ts"
    ));

    protected FileFilter fileFilter = pathname -> {
        String name = pathname.getName();
        int i = name.lastIndexOf('.');
        if (i == -1 || i == name.length() - 1)
            return false;
        String extension = name.substring(i + 1);
        return extensions.contains(extension.toLowerCase());
    };

    public M3UHandler(String workDir, String prefix, TokenFilter tokenFilter, String name) {
        super(workDir, prefix, tokenFilter, name);
    }

    @Override
    protected Response handleDirectory(Request request, Response response, String path, File file) {
        StringBuilder sb = new StringBuilder();
        HandlerContextWithToken handlerContext = createHandlerContext(path, request);
        return response.setBody(addFileRecursively(file, sb, handlerContext).toString())
                .header(Header.KEY_CONTENT_TYPE, "audio/x-mpegurl");
    }

    protected StringBuilder addFileRecursively(File file, StringBuilder sb, HandlerContextWithToken context) {
        if (file.isDirectory()) {
            File[] files = file.listFiles(fileFilter);
            if (files != null)
                for (File f : files) {
                    addFileRecursively(f, sb, context);
                }
        } else {
            sb.append(generateUrl(file, context)).append("\n");
        }
        return sb;
    }

}

@@ -403,16 +403,64 @@
 UHandler
+%3CT extends M3UHandler.HandlerContextWithRequest%3E
  extends
@@ -484,16 +484,19 @@
 eHandler
+%3CT%3E
  %7B%0A%0A    
@@ -1385,39 +1385,17 @@
         
-HandlerContextWithToken
+T
  handler
@@ -1638,439 +1638,1412 @@
 der 
-addFileRecursively(File file, StringBuilder sb, HandlerContextWithToken context) %7B%0A        if (file.isDirectory()) %7B%0A            File%5B%5D files = file.listFiles(fileFilter);%0A            if (files != null)%0A                for (File f : files) %7B%0A                    addFileRecursively(f, sb, context);%0A                %7D%0A        %7D else %7B%0A            sb.append(generateUrl(file, context)).append(%22%5Cn%22);%0A        %7D%0A        return sb;%0A    %7D%0A
+getPath(StringBuilder sb, File file) %7B%0A        if (!file.equals(workDir)) %7B%0A            getPath(sb, file.getParentFile());%0A            sb.append(%22/%22).append(encodeName(file.getName()));%0A        %7D%0A        return sb;%0A    %7D%0A%0A    @Override%0A    protected String generateUrl(File file, T handlerContext) %7B%0A        StringBuilder sb = new StringBuilder(%22https://%22 + handlerContext.request.header(Header.KEY_HOST));%0A        getPath(sb, file).append(%22/%22).append(super.generateUrl(file, handlerContext));%0A        return sb.toString();%0A    %7D%0A%0A    protected StringBuilder addFileRecursively(File file, StringBuilder sb, T context) %7B%0A        if (file.isDirectory()) %7B%0A            File%5B%5D files = file.listFiles(fileFilter);%0A            if (files != null)%0A                for (File f : files) %7B%0A                    addFileRecursively(f, sb, context);%0A                %7D%0A        %7D else %7B%0A            sb.append(generateUrl(file, context)).append(%22%5Cn%22);%0A        %7D%0A        return sb;%0A    %7D%0A%0A    @Override%0A    protected T createHandlerContext(String path, Request request) %7B%0A        return (T) new HandlerContextWithRequest(path, request);%0A    %7D%0A%0A    protected class HandlerContextWithRequest extends HandlerContextWithToken %7B%0A        protected final Request request;%0A%0A        public HandlerContextWithRequest(String path, Request request) %7B%0A            super(path, request);%0A            this.request = request;%0A        %7D%0A    %7D
 %0A%7D%0A

generate absolute paths