OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 149 Rev 156
Line 11... Line 11...
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.erp.core.edm;
14
 package org.openconcerto.erp.core.edm;
15
 
15
 
16
import java.io.BufferedInputStream;
-
 
17
import java.io.File;
-
 
18
import java.io.FileInputStream;
-
 
19
import java.io.IOException;
-
 
20
import java.sql.SQLException;
-
 
21
import java.util.Collection;
-
 
22
import java.util.List;
-
 
23
 
-
 
24
import javax.swing.JOptionPane;
-
 
25
 
-
 
26
import eu.medsea.mimeutil.MimeType;
-
 
27
import eu.medsea.mimeutil.MimeUtil;
-
 
28
import org.openconcerto.erp.config.ComptaPropsConfiguration;
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
29
import org.openconcerto.erp.generationDoc.DocumentLocalStorageManager;
17
import org.openconcerto.erp.generationDoc.DocumentLocalStorageManager;
30
import org.openconcerto.erp.storage.StorageEngine;
18
import org.openconcerto.erp.storage.StorageEngine;
31
import org.openconcerto.erp.storage.StorageEngines;
19
import org.openconcerto.erp.storage.StorageEngines;
32
import org.openconcerto.sql.model.SQLRow;
20
import org.openconcerto.sql.model.SQLRow;
Line 35... Line 23...
35
import org.openconcerto.sql.model.SQLTable;
23
import org.openconcerto.sql.model.SQLTable;
36
import org.openconcerto.utils.ExceptionHandler;
24
import org.openconcerto.utils.ExceptionHandler;
37
import org.openconcerto.utils.FileUtils;
25
import org.openconcerto.utils.FileUtils;
38
import org.openconcerto.utils.sync.SyncClient;
26
import org.openconcerto.utils.sync.SyncClient;
39
 
27
 
-
 
28
import java.io.BufferedInputStream;
-
 
29
import java.io.File;
-
 
30
import java.io.FileInputStream;
-
 
31
import java.io.IOException;
-
 
32
import java.nio.file.Files;
-
 
33
import java.sql.SQLException;
-
 
34
import java.util.List;
-
 
35
 
-
 
36
import javax.swing.JOptionPane;
-
 
37
 
40
public class AttachmentUtils {
38
public class AttachmentUtils {
41
 
39
 
42
    public void uploadFile(File inFile, SQLRowAccessor rowSource, int idParent) {
40
    public void uploadFile(File inFile, SQLRowAccessor rowSource, int idParent) {
43
        try {
41
        try {
44
 
42
 
Line 49... Line 47...
49
            rowValsAttachment.put("ID_PARENT", idParent);
47
            rowValsAttachment.put("ID_PARENT", idParent);
50
            SQLRow rowAttachment = rowValsAttachment.insert();
48
            SQLRow rowAttachment = rowValsAttachment.insert();
51
            int id = rowAttachment.getID();
49
            int id = rowAttachment.getID();
52
 
50
 
53
            final String folderId = String.valueOf((id / 1000) * 1000);
51
            final String folderId = String.valueOf((id / 1000) * 1000);
54
            String subDir = "EDM" + File.separator + folderId;
52
            String subDir = "EDM/" + folderId;
55
            String fileNameID = String.valueOf(id);
53
            String fileNameID = String.valueOf(id);
56
            String ext = "";
54
            String ext = "";
57
 
55
 
58
            int i = inFile.getName().lastIndexOf('.');
56
            int i = inFile.getName().lastIndexOf('.');
59
            if (i > 0) {
57
            if (i > 0) {
Line 66... Line 64...
66
            boolean isOnCloud = config.isOnCloud();
64
            boolean isOnCloud = config.isOnCloud();
67
 
65
 
68
            if (isOnCloud) {
66
            if (isOnCloud) {
69
 
67
 
70
                String remotePath = subDir;
68
                String remotePath = subDir;
71
                remotePath = remotePath.replace('\\', '/');
-
 
72
                List<StorageEngine> engines = StorageEngines.getInstance().getActiveEngines();
69
                List<StorageEngine> engines = StorageEngines.getInstance().getActiveEngines();
73
                for (StorageEngine storageEngine : engines) {
70
                for (StorageEngine storageEngine : engines) {
74
                    if (storageEngine.isConfigured() && storageEngine.allowAutoStorage()) {
71
                    if (storageEngine.isConfigured() && storageEngine.allowAutoStorage()) {
75
                        final String path = remotePath;
72
                        final String path = remotePath;
76
 
73
 
Line 122... Line 119...
122
                name = name.substring(0, index);
119
                name = name.substring(0, index);
123
            }
120
            }
124
            rowValsAttachment.put("NAME", name);
121
            rowValsAttachment.put("NAME", name);
125
            rowValsAttachment.put("SOURCE_TABLE", rowSource.getTable().getName());
122
            rowValsAttachment.put("SOURCE_TABLE", rowSource.getTable().getName());
126
            rowValsAttachment.put("SOURCE_ID", rowSource.getID());
123
            rowValsAttachment.put("SOURCE_ID", rowSource.getID());
127
            Collection<MimeType> mimeTypes = MimeUtil.getMimeTypes(inFile);
-
 
128
            if (mimeTypes != null && !mimeTypes.isEmpty()) {
-
 
129
                final MimeType mimeType = (MimeType) mimeTypes.toArray()[0];
124
            final String mimeType = Files.probeContentType(inFile.toPath());
130
                rowValsAttachment.put("MIMETYPE", mimeType.getMediaType() + "/" + mimeType.getSubType());
-
 
131
            } else {
-
 
132
                rowValsAttachment.put("MIMETYPE", "application/octet-stream");
125
            rowValsAttachment.put("MIMETYPE", mimeType != null ? mimeType : "application/octet-stream");
133
            }
-
 
134
            rowValsAttachment.put("FILENAME", fileName);
126
            rowValsAttachment.put("FILENAME", fileName);
135
            rowValsAttachment.put("FILESIZE", inFile.length());
127
            rowValsAttachment.put("FILESIZE", inFile.length());
136
            rowValsAttachment.put("STORAGE_PATH", subDir);
128
            rowValsAttachment.put("STORAGE_PATH", subDir);
137
            rowValsAttachment.put("STORAGE_FILENAME", fileWithIDNAme);
129
            rowValsAttachment.put("STORAGE_FILENAME", fileWithIDNAme);
138
            // TODO THUMBNAIL
130
            // TODO THUMBNAIL
Line 187... Line 179...
187
        } else {
179
        } else {
188
 
180
 
189
            // Get file out
181
            // Get file out
190
            File dirRoot = DocumentLocalStorageManager.getInstance().getDocumentOutputDirectory(AttachmentSQLElement.DIRECTORY_PREFS);
182
            File dirRoot = DocumentLocalStorageManager.getInstance().getDocumentOutputDirectory(AttachmentSQLElement.DIRECTORY_PREFS);
191
            File storagePathFile = new File(dirRoot, subDir);
183
            File storagePathFile = new File(dirRoot, subDir);
-
 
184
            File fileIn;
-
 
185
            try {
192
            File fileIn = new File(storagePathFile, fileName);
186
                fileIn = new File(storagePathFile, fileName).getCanonicalFile();
193
            if (fileIn.exists()) {
187
                if (fileIn.exists()) {
194
                final File outFile = new File(f, fileName);
188
                    final File outFile = new File(f, fileName);
195
                try {
189
                    try {
196
                    FileUtils.copyFile(fileIn, outFile);
190
                        FileUtils.copyFile(fileIn, outFile);
197
                } catch (IOException e) {
191
                    } catch (IOException e) {
198
                    ExceptionHandler.handle("Impossible de copier le fichier vers le fichier temporaire de réception", e);
192
                        ExceptionHandler.handle("Impossible de copier le fichier vers le fichier temporaire de réception", e);
199
                    return null;
193
                        return null;
200
                }
194
                    }
201
            } else {
195
                } else {
202
                JOptionPane.showMessageDialog(null, "Le fichier n'existe pas sur le serveur!", "Erreur fichier", JOptionPane.ERROR_MESSAGE);
196
                    JOptionPane.showMessageDialog(null, "Le fichier n'existe pas sur le serveur!\n" + fileIn.getAbsolutePath(), "Erreur fichier", JOptionPane.ERROR_MESSAGE);
-
 
197
                    return null;
-
 
198
                }
-
 
199
            } catch (IOException e1) {
-
 
200
                ExceptionHandler.handle("Impossible de trouver le fichier\n" + storagePathFile + File.pathSeparator + fileName, e1);
-
 
201
                return null;
203
            }
202
            }
204
        }
203
        }
205
        final File outFile = new File(f, fileName);
204
        final File outFile = new File(f, fileName);
206
        outFile.setReadOnly();
205
        outFile.setReadOnly();
207
        return outFile;
206
        return outFile;