package com.shinewave.sopviewer;

import android.os.Environment;

import com.shinewave.sopviewer.ServerConnectionInfo;
import com.artifex.mupdfdemo.AsyncTask;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Date;

import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;

/**
 * Created by user on 2015/12/2.
 */
public class ServerDownloadSmbFile extends AsyncTask<ServerConnectionInfo, Integer, String> {
    private SmbFile smbFile;
    private ServerConnectionInfo info;
    private String localPath;

    @Override
    protected String doInBackground(ServerConnectionInfo... params)
    {
        try {
            info = params[0];
            NtlmPasswordAuthentication authentication = new NtlmPasswordAuthentication("", info.id, info.password); // domain, user, password
            smbFile = new SmbFile("smb://" + info.url + info.fullFilePath, authentication);
            if (smbFile != null) {
                localPath = Environment.getExternalStorageDirectory() + File.separator + smbFile.getName();
                FileOutputStream fileOutputStream = new FileOutputStream(localPath);
                InputStream fileInputStream = smbFile.getInputStream();
                byte[] buf = new byte[16 * 1024 * 1024];
                int len;
                while ((len = fileInputStream.read(buf)) > 0) {
                    fileOutputStream.write(buf, 0, len);
                }
                fileInputStream.close();
                fileOutputStream.close();
            }
            return localPath;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    protected void onPostExecute(String result)
    {
        super.onPostExecute(result);

        if(result == null)
            return;

        ConnectionInfo connInfo = new ConnectionInfo();
        connInfo.connectionName = info.connectionName;
        connInfo.protocol = info.protocol;
        connInfo.url = info.url;
        connInfo.id = info.id;
        connInfo.password = info.password;
        ConnectionInfo tmpConn = DBManager.getConnection(info.connectionName);
        if(tmpConn != null)
            DBManager.updateConnection(connInfo);
        else
            DBManager.insertConnection(connInfo);

        FileInfo fileInfo = new FileInfo();
        fileInfo.localFullFilePath = result;
        fileInfo.remoteFullFilePath = smbFile.getPath();
        fileInfo.connectionName = info.connectionName;
        fileInfo.updateTime = new Date(System.currentTimeMillis());
        fileInfo.syncSucceed = true;
        fileInfo.size = (int)result.length();
        try {
            fileInfo.remoteTimeStamp = new Date(smbFile.lastModified());
        }
        catch(Exception e)
        {}

        FileInfo tmp = DBManager.getSingleFileInfo(fileInfo.localFullFilePath);
        if(tmp != null && tmp.localFullFilePath != null)
            DBManager.updateFileInfo(fileInfo);
        else
            DBManager.insertFileInfo(fileInfo);
    }

}

@@ -839,16 +839,71 @@
 assword%0A
+%0A            if(info.fullFilePath.startsWith(%22/%22))%0A    
         
@@ -1002,100 +1002,355 @@
     
-if (smbFile != null) %7B%0A                localPath = Environment.getExternalStorageDirectory()
+else%0A                smbFile = new SmbFile(%22smb://%22 + info.url + File.separator + info.fullFilePath, authentication);%0A%0A            if (smbFile != null) %7B%0A                if(info.fileSavePath.endsWith(%22/%22))%0A                    localPath = info.fileSavePath + smbFile.getName();%0A                else%0A                    localPath = info.fileSavePath
  + F
@@ -1376,32 +1376,33 @@
 File.getName();%0A
+%0A
                 

fix ServerDownloadSmbFile檔案路徑問題