001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.store;
016    
017    import com.liferay.portal.kernel.bean.BeanReference;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.io.ByteArrayFileInputStream;
021    import com.liferay.portal.kernel.util.FileUtil;
022    import com.liferay.portal.service.GroupLocalService;
023    import com.liferay.portal.util.PropsValues;
024    import com.liferay.portlet.documentlibrary.DirectoryNameException;
025    import com.liferay.portlet.documentlibrary.antivirus.AntivirusScannerUtil;
026    import com.liferay.portlet.documentlibrary.util.DLValidatorUtil;
027    
028    import java.io.File;
029    import java.io.IOException;
030    import java.io.InputStream;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     * @author Alexander Chow
035     * @author Edward Han
036     */
037    public class DLStoreImpl implements DLStore {
038    
039            @Override
040            public void addDirectory(long companyId, long repositoryId, String dirName)
041                    throws PortalException {
042    
043                    if (!DLValidatorUtil.isValidName(dirName) || dirName.equals("/")) {
044                            throw new DirectoryNameException(dirName);
045                    }
046    
047                    store.addDirectory(companyId, repositoryId, dirName);
048            }
049    
050            @Override
051            public void addFile(
052                            long companyId, long repositoryId, String fileName,
053                            boolean validateFileExtension, byte[] bytes)
054                    throws PortalException {
055    
056                    validate(fileName, validateFileExtension);
057    
058                    if (PropsValues.DL_STORE_ANTIVIRUS_ENABLED) {
059                            AntivirusScannerUtil.scan(bytes);
060                    }
061    
062                    store.addFile(companyId, repositoryId, fileName, bytes);
063            }
064    
065            @Override
066            public void addFile(
067                            long companyId, long repositoryId, String fileName,
068                            boolean validateFileExtension, File file)
069                    throws PortalException {
070    
071                    validate(fileName, validateFileExtension);
072    
073                    if (PropsValues.DL_STORE_ANTIVIRUS_ENABLED) {
074                            AntivirusScannerUtil.scan(file);
075                    }
076    
077                    store.addFile(companyId, repositoryId, fileName, file);
078            }
079    
080            @Override
081            public void addFile(
082                            long companyId, long repositoryId, String fileName,
083                            boolean validateFileExtension, InputStream is)
084                    throws PortalException {
085    
086                    if (is instanceof ByteArrayFileInputStream) {
087                            ByteArrayFileInputStream byteArrayFileInputStream =
088                                    (ByteArrayFileInputStream)is;
089    
090                            File file = byteArrayFileInputStream.getFile();
091    
092                            addFile(
093                                    companyId, repositoryId, fileName, validateFileExtension, file);
094    
095                            return;
096                    }
097    
098                    validate(fileName, validateFileExtension);
099    
100                    if (!PropsValues.DL_STORE_ANTIVIRUS_ENABLED ||
101                            !AntivirusScannerUtil.isActive()) {
102    
103                            store.addFile(companyId, repositoryId, fileName, is);
104                    }
105                    else {
106                            File tempFile = null;
107    
108                            try {
109                                    if (is.markSupported()) {
110                                            is.mark(is.available() + 1);
111    
112                                            AntivirusScannerUtil.scan(is);
113    
114                                            is.reset();
115    
116                                            store.addFile(companyId, repositoryId, fileName, is);
117                                    }
118                                    else {
119                                            tempFile = FileUtil.createTempFile();
120    
121                                            FileUtil.write(tempFile, is);
122    
123                                            AntivirusScannerUtil.scan(tempFile);
124    
125                                            store.addFile(companyId, repositoryId, fileName, tempFile);
126                                    }
127                            }
128                            catch (IOException ioe) {
129                                    throw new SystemException(
130                                            "Unable to scan file " + fileName, ioe);
131                            }
132                            finally {
133                                    if (tempFile != null) {
134                                            tempFile.delete();
135                                    }
136                            }
137                    }
138            }
139    
140            @Override
141            public void addFile(
142                            long companyId, long repositoryId, String fileName, byte[] bytes)
143                    throws PortalException {
144    
145                    addFile(companyId, repositoryId, fileName, true, bytes);
146            }
147    
148            @Override
149            public void addFile(
150                            long companyId, long repositoryId, String fileName, File file)
151                    throws PortalException {
152    
153                    addFile(companyId, repositoryId, fileName, true, file);
154            }
155    
156            @Override
157            public void addFile(
158                            long companyId, long repositoryId, String fileName, InputStream is)
159                    throws PortalException {
160    
161                    addFile(companyId, repositoryId, fileName, true, is);
162            }
163    
164            @Override
165            public void checkRoot(long companyId) {
166                    store.checkRoot(companyId);
167            }
168    
169            @Override
170            public void copyFileVersion(
171                            long companyId, long repositoryId, String fileName,
172                            String fromVersionLabel, String toVersionLabel)
173                    throws PortalException {
174    
175                    store.copyFileVersion(
176                            companyId, repositoryId, fileName, fromVersionLabel,
177                            toVersionLabel);
178            }
179    
180            @Override
181            public void deleteDirectory(
182                    long companyId, long repositoryId, String dirName) {
183    
184                    store.deleteDirectory(companyId, repositoryId, dirName);
185            }
186    
187            @Override
188            public void deleteFile(long companyId, long repositoryId, String fileName)
189                    throws PortalException {
190    
191                    validate(fileName, false);
192    
193                    store.deleteFile(companyId, repositoryId, fileName);
194            }
195    
196            @Override
197            public void deleteFile(
198                            long companyId, long repositoryId, String fileName,
199                            String versionLabel)
200                    throws PortalException {
201    
202                    validate(fileName, false, versionLabel);
203    
204                    store.deleteFile(companyId, repositoryId, fileName, versionLabel);
205            }
206    
207            @Override
208            public File getFile(long companyId, long repositoryId, String fileName)
209                    throws PortalException {
210    
211                    validate(fileName, false);
212    
213                    return store.getFile(companyId, repositoryId, fileName);
214            }
215    
216            @Override
217            public File getFile(
218                            long companyId, long repositoryId, String fileName,
219                            String versionLabel)
220                    throws PortalException {
221    
222                    validate(fileName, false, versionLabel);
223    
224                    return store.getFile(companyId, repositoryId, fileName, versionLabel);
225            }
226    
227            @Override
228            public byte[] getFileAsBytes(
229                            long companyId, long repositoryId, String fileName)
230                    throws PortalException {
231    
232                    validate(fileName, false);
233    
234                    return store.getFileAsBytes(companyId, repositoryId, fileName);
235            }
236    
237            @Override
238            public byte[] getFileAsBytes(
239                            long companyId, long repositoryId, String fileName,
240                            String versionLabel)
241                    throws PortalException {
242    
243                    validate(fileName, false, versionLabel);
244    
245                    return store.getFileAsBytes(
246                            companyId, repositoryId, fileName, versionLabel);
247            }
248    
249            @Override
250            public InputStream getFileAsStream(
251                            long companyId, long repositoryId, String fileName)
252                    throws PortalException {
253    
254                    validate(fileName, false);
255    
256                    return store.getFileAsStream(companyId, repositoryId, fileName);
257            }
258    
259            @Override
260            public InputStream getFileAsStream(
261                            long companyId, long repositoryId, String fileName,
262                            String versionLabel)
263                    throws PortalException {
264    
265                    validate(fileName, false, versionLabel);
266    
267                    return store.getFileAsStream(
268                            companyId, repositoryId, fileName, versionLabel);
269            }
270    
271            @Override
272            public String[] getFileNames(
273                            long companyId, long repositoryId, String dirName)
274                    throws PortalException {
275    
276                    if (!DLValidatorUtil.isValidName(dirName)) {
277                            throw new DirectoryNameException(dirName);
278                    }
279    
280                    return store.getFileNames(companyId, repositoryId, dirName);
281            }
282    
283            @Override
284            public long getFileSize(long companyId, long repositoryId, String fileName)
285                    throws PortalException {
286    
287                    validate(fileName, false);
288    
289                    return store.getFileSize(companyId, repositoryId, fileName);
290            }
291    
292            @Override
293            public boolean hasDirectory(
294                            long companyId, long repositoryId, String dirName)
295                    throws PortalException {
296    
297                    if (!DLValidatorUtil.isValidName(dirName)) {
298                            throw new DirectoryNameException(dirName);
299                    }
300    
301                    return store.hasDirectory(companyId, repositoryId, dirName);
302            }
303    
304            @Override
305            public boolean hasFile(long companyId, long repositoryId, String fileName)
306                    throws PortalException {
307    
308                    validate(fileName, false);
309    
310                    return store.hasFile(companyId, repositoryId, fileName);
311            }
312    
313            @Override
314            public boolean hasFile(
315                            long companyId, long repositoryId, String fileName,
316                            String versionLabel)
317                    throws PortalException {
318    
319                    validate(fileName, false, versionLabel);
320    
321                    return store.hasFile(companyId, repositoryId, fileName, versionLabel);
322            }
323    
324            /**
325             * @deprecated As of 7.0.0, replaced by {@link
326             *             DLValidatorUtil#isValidName(String)}
327             */
328            @Deprecated
329            @Override
330            public boolean isValidName(String name) {
331                    return DLValidatorUtil.isValidName(name);
332            }
333    
334            @Override
335            public void move(String srcDir, String destDir) {
336                    store.move(srcDir, destDir);
337            }
338    
339            @Override
340            public void updateFile(
341                            long companyId, long repositoryId, long newRepositoryId,
342                            String fileName)
343                    throws PortalException {
344    
345                    store.updateFile(companyId, repositoryId, newRepositoryId, fileName);
346            }
347    
348            @Override
349            public void updateFile(
350                            long companyId, long repositoryId, String fileName,
351                            String newFileName)
352                    throws PortalException {
353    
354                    store.updateFile(companyId, repositoryId, fileName, newFileName);
355            }
356    
357            @Override
358            public void updateFile(
359                            long companyId, long repositoryId, String fileName,
360                            String fileExtension, boolean validateFileExtension,
361                            String versionLabel, String sourceFileName, File file)
362                    throws PortalException {
363    
364                    validate(
365                            fileName, fileExtension, sourceFileName, validateFileExtension);
366    
367                    DLValidatorUtil.validateVersionLabel(versionLabel);
368    
369                    if (PropsValues.DL_STORE_ANTIVIRUS_ENABLED) {
370                            AntivirusScannerUtil.scan(file);
371                    }
372    
373                    store.updateFile(companyId, repositoryId, fileName, versionLabel, file);
374            }
375    
376            @Override
377            public void updateFile(
378                            long companyId, long repositoryId, String fileName,
379                            String fileExtension, boolean validateFileExtension,
380                            String versionLabel, String sourceFileName, InputStream is)
381                    throws PortalException {
382    
383                    if (is instanceof ByteArrayFileInputStream) {
384                            ByteArrayFileInputStream byteArrayFileInputStream =
385                                    (ByteArrayFileInputStream)is;
386    
387                            File file = byteArrayFileInputStream.getFile();
388    
389                            updateFile(
390                                    companyId, repositoryId, fileName, fileExtension,
391                                    validateFileExtension, versionLabel, sourceFileName, file);
392    
393                            return;
394                    }
395    
396                    validate(
397                            fileName, fileExtension, sourceFileName, validateFileExtension);
398    
399                    DLValidatorUtil.validateVersionLabel(versionLabel);
400    
401                    if (!PropsValues.DL_STORE_ANTIVIRUS_ENABLED ||
402                            !AntivirusScannerUtil.isActive()) {
403    
404                            store.updateFile(
405                                    companyId, repositoryId, fileName, versionLabel, is);
406                    }
407                    else {
408                            File tempFile = null;
409    
410                            try {
411                                    if (is.markSupported()) {
412                                            is.mark(is.available() + 1);
413    
414                                            AntivirusScannerUtil.scan(is);
415    
416                                            is.reset();
417    
418                                            store.updateFile(
419                                                    companyId, repositoryId, fileName, versionLabel, is);
420                                    }
421                                    else {
422                                            tempFile = FileUtil.createTempFile();
423    
424                                            FileUtil.write(tempFile, is);
425    
426                                            AntivirusScannerUtil.scan(tempFile);
427    
428                                            store.updateFile(
429                                                    companyId, repositoryId, fileName, versionLabel,
430                                                    tempFile);
431                                    }
432                            }
433                            catch (IOException ioe) {
434                                    throw new SystemException(
435                                            "Unable to scan file " + fileName, ioe);
436                            }
437                            finally {
438                                    if (tempFile != null) {
439                                            tempFile.delete();
440                                    }
441                            }
442                    }
443            }
444    
445            @Override
446            public void updateFileVersion(
447                            long companyId, long repositoryId, String fileName,
448                            String fromVersionLabel, String toVersionLabel)
449                    throws PortalException {
450    
451                    store.updateFileVersion(
452                            companyId, repositoryId, fileName, fromVersionLabel,
453                            toVersionLabel);
454            }
455    
456            @Override
457            public void validate(String fileName, boolean validateFileExtension)
458                    throws PortalException {
459    
460                    DLValidatorUtil.validateFileName(fileName);
461    
462                    if (validateFileExtension) {
463                            DLValidatorUtil.validateFileExtension(fileName);
464                    }
465            }
466    
467            @Override
468            public void validate(
469                            String fileName, boolean validateFileExtension, byte[] bytes)
470                    throws PortalException {
471    
472                    validate(fileName, validateFileExtension);
473    
474                    DLValidatorUtil.validateFileSize(fileName, bytes);
475            }
476    
477            @Override
478            public void validate(
479                            String fileName, boolean validateFileExtension, File file)
480                    throws PortalException {
481    
482                    validate(fileName, validateFileExtension);
483    
484                    DLValidatorUtil.validateFileSize(fileName, file);
485            }
486    
487            @Override
488            public void validate(
489                            String fileName, boolean validateFileExtension, InputStream is)
490                    throws PortalException {
491    
492                    validate(fileName, validateFileExtension);
493    
494                    DLValidatorUtil.validateFileSize(fileName, is);
495            }
496    
497            @Override
498            public void validate(
499                            String fileName, String fileExtension, String sourceFileName,
500                            boolean validateFileExtension)
501                    throws PortalException {
502    
503                    validate(fileName, validateFileExtension);
504    
505                    DLValidatorUtil.validateSourceFileExtension(
506                            fileExtension, sourceFileName);
507            }
508    
509            @Override
510            public void validate(
511                            String fileName, String fileExtension, String sourceFileName,
512                            boolean validateFileExtension, File file)
513                    throws PortalException {
514    
515                    validate(
516                            fileName, fileExtension, sourceFileName, validateFileExtension);
517    
518                    DLValidatorUtil.validateFileSize(fileName, file);
519            }
520    
521            @Override
522            public void validate(
523                            String fileName, String fileExtension, String sourceFileName,
524                            boolean validateFileExtension, InputStream is)
525                    throws PortalException {
526    
527                    validate(
528                            fileName, fileExtension, sourceFileName, validateFileExtension);
529    
530                    DLValidatorUtil.validateFileSize(fileName, is);
531            }
532    
533            /**
534             * @deprecated As of 7.0.0, replaced by {@link
535             *             DLValidatorUtil#validateDirectoryName(String)}
536             */
537            @Deprecated
538            @Override
539            public void validateDirectoryName(String directoryName)
540                    throws PortalException {
541    
542                    DLValidatorUtil.validateDirectoryName(directoryName);
543            }
544    
545            protected void validate(
546                            String fileName, boolean validateFileExtension, String versionLabel)
547                    throws PortalException {
548    
549                    validate(fileName, validateFileExtension);
550    
551                    DLValidatorUtil.validateVersionLabel(versionLabel);
552            }
553    
554            protected void validate(
555                            String fileName, String fileExtension, String sourceFileName,
556                            boolean validateFileExtension, File file, String versionLabel)
557                    throws PortalException {
558    
559                    validate(
560                            fileName, fileExtension, sourceFileName, validateFileExtension,
561                            file);
562    
563                    DLValidatorUtil.validateVersionLabel(versionLabel);
564            }
565    
566            protected void validate(
567                            String fileName, String fileExtension, String sourceFileName,
568                            boolean validateFileExtension, InputStream is, String versionLabel)
569                    throws PortalException {
570    
571                    validate(
572                            fileName, fileExtension, sourceFileName, validateFileExtension, is);
573    
574                    DLValidatorUtil.validateVersionLabel(versionLabel);
575            }
576    
577            @BeanReference(type = GroupLocalService.class)
578            protected GroupLocalService groupLocalService;
579    
580            @BeanReference(type = Store.class)
581            protected Store store;
582    
583    }