Kenmegne
7 days ago 23a46b4be35277e06ec89f48730eeb694e686be8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.megatim.apifdxweb.impl.tools;
 
import com.megatim.apifdxweb.tools.AppContext;
import com.megatim.apifdxweb.tools.exceptions.ApplicationServerException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.ws.rs.core.MultivaluedMap;
import org.jboss.resteasy.plugins.providers.multipart.InputPart;
 
/**
 *
 * @author ASUS
 */
public class Utilities {
 
//    public static boolean arePropertiesOk() {
//        boolean dirOk = AppContext.INTEGRATION_STANDALONE_DESTINATION_DIR != null && !AppContext.INTEGRATION_STANDALONE_DESTINATION_DIR.isEmpty()
//                && AppContext.INTEGRATION_STANDALONE_TEMP_DIR != null && !AppContext.INTEGRATION_STANDALONE_TEMP_DIR.isEmpty()
//                && Files.exists(Paths.get(AppContext.INTEGRATION_STANDALONE_TEMP_DIR));
//        System.out.println("dirOk = " + dirOk);
//
//        if (!AppContext.INTEGRATION_STANDALONE_IS_REMOTE) {
//            return dirOk;
//        } else {
//            boolean paramsOk = AppContext.INTEGRATION_STANDALONE_HOSTNAME != null && !AppContext.INTEGRATION_STANDALONE_HOSTNAME.isEmpty()
//                    && AppContext.INTEGRATION_STANDALONE_PASSWORD != null && !AppContext.INTEGRATION_STANDALONE_PASSWORD.isEmpty()
//                    && AppContext.INTEGRATION_STANDALONE_USERNAME != null && !AppContext.INTEGRATION_STANDALONE_USERNAME.isEmpty();
//
//            System.out.println("paramsOk = " + paramsOk);
//            return dirOk && paramsOk;
//        }
//    }
 
    public static File saveFileToDisk(List<InputPart> inputParts) throws ApplicationServerException {
 
        int bufferSize = 1024;
        File customDir = new File(AppContext.UPLOAD_DIR);
 
        //Si le repertoire n'existe pas, on crée
        if (!customDir.exists()) {
            //On crée le repertoire
            customDir.mkdir();
        }
 
        if (inputParts != null) {
            for (InputPart inputPart : inputParts) {
                MultivaluedMap<String, String> header = inputPart.getHeaders();
                try {
                    String fileName = getFileName(header);
                    InputStream inputStream = inputPart.getBody(InputStream.class, null);
                    fileName = customDir + File.separator + fileName;
                    File file = writeFile(inputStream, fileName, bufferSize);
                    return file;
                } catch (IOException ex) {
                    ex.printStackTrace();
                    throw new ApplicationServerException();
                }
            }
        }
        return null;
    }
 
    private static String getFileName(MultivaluedMap<String, String> header) {
        String[] contentDisposition = header.getFirst("Content-Disposition").split(";");
        for (String filename : contentDisposition) {
            if ((filename.trim().startsWith("filename"))) {
                String[] name = filename.split("=");
                String finalFileName = name[1].trim().replaceAll("\"", "");
                return finalFileName;
            }
        }
        return null;
    }
 
    private static File writeFile(InputStream input, String filename, int bufferSize) throws IOException {
 
        File file = new File(filename);
        if (!file.exists()) {
            file.createNewFile();
        }
 
        try ( FileOutputStream fop = new FileOutputStream(file)) {
            byte[] buffer = new byte[bufferSize];
            int n = 0;
            while (-1 != (n = input.read(buffer))) {
                fop.write(buffer, 0, n);
            }
            fop.flush();
        }
 
        return file;
    }
}