package com.ettrema.http.fs;

import com.bradmcevoy.common.ContentTypeUtils;
import com.bradmcevoy.http.Auth;
import com.bradmcevoy.http.CopyableResource;
import com.bradmcevoy.http.DeletableResource;
import com.bradmcevoy.http.GetableResource;
import com.bradmcevoy.http.MoveableResource;
import com.bradmcevoy.http.PropFindableResource;
import com.bradmcevoy.http.Range;
import com.bradmcevoy.http.Request;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 */
public class FsFileResource extends FsResource implements CopyableResource, DeletableResource, GetableResource, MoveableResource, PropFindableResource {

    private static final Logger log = LoggerFactory.getLogger( FsFileResource.class );

    /**
     *
     * @param host - the requested host. Eg www.mycompany.com
     * @param factory
     * @param file
     */
    public FsFileResource( String host, FileSystemResourceFactory factory, File file ) {
        super( host, factory, file );
    }

    public Long getContentLength() {
        return file.length();
    }

    public String getContentType( String preferredList ) {
        String mime = ContentTypeUtils.findContentTypes( this.file );
        return ContentTypeUtils.findAcceptableContentType( mime, preferredList );
    }

    public String checkRedirect( Request arg0 ) {
        return null;
    }

    public void sendContent( OutputStream out, Range range, Map<String, String> params, String contentType ) throws IOException {
        log.debug( "send content: " + file.getAbsolutePath() );
        FileInputStream in = null;
        try {
            in = new FileInputStream( file );
            //        if( range != null ) {
            //            long start = range.getStart();
            //            if( start > 0 ) in.skip(start);
            //            long finish = range.getFinish();
            //            if( finish > 0 ) {
            //                StreamToStream.readTo(in, out);
            //            }
            //        } else {
            int bytes = IOUtils.copy( in, out );
            log.debug( "wrote bytes:  " + bytes );
            out.flush();
        //        }
        } finally {
            IOUtils.closeQuietly( in );
        }
    }

    /** @{@inheritDoc} */
    public Long getMaxAgeSeconds(Auth auth) {
        return factory.maxAgeSeconds( this );
    }

    /** @{@inheritDoc} */
    @Override
    protected void doCopy( File dest ) {
        try {
            FileUtils.copyFile( file, dest );
        } catch( IOException ex ) {
            throw new RuntimeException( "Failed doing copy to: " + dest.getAbsolutePath(), ex );
        }
    }
}

@@ -363,49 +363,158 @@
 ttp.
-Range;%0Aimport com.bradmcevoy.http.Request
+PropPatchableResource;%0Aimport com.bradmcevoy.http.Range;%0Aimport com.bradmcevoy.http.Request;%0Aimport com.bradmcevoy.http.webdav.PropPatchHandler.Fields
 ;%0Aim
@@ -940,16 +940,39 @@
 Resource
+, PropPatchableResource
  %7B%0A%0A    
@@ -2483,32 +2483,36 @@
 lush();%0A        
+    
 //        %7D%0A    
@@ -2643,16 +2643,17 @@
 Seconds(
+ 
 Auth aut
@@ -2653,16 +2653,17 @@
 uth auth
+ 
 ) %7B%0A    
@@ -3000,11 +3000,142 @@
  %7D%0A    %7D
+%0A%0A    public void setProperties( Fields fields ) %7B%0A        // MIL-50%0A        // not implemented. Just to keep MS Office sweet%0A    %7D
 %0A%7D%0A

implement PROPPATH for MS Office support