001/*
002 * (C) Copyright 2018 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     dmetzler
018 */
019package org.nuxeo.io.fsexporter;
020
021import java.io.File;
022import java.io.IOException;
023import java.util.regex.Pattern;
024
025import org.apache.commons.lang3.StringUtils;
026import org.nuxeo.ecm.core.api.Blob;
027import org.nuxeo.ecm.core.api.CoreSession;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
030
031/**
032 * Plugin for FS Exporter that exports documents like they should appear in Nuxeo Drive.
033 *
034 * @since 10.3
035 */
036public class DriveLikeExporterPlugin extends DefaultExporterPlugin {
037
038    public static final Pattern FORBIDDEN_CHAR_PATTERN = Pattern.compile("[\\\"|*/:<>?\\\\]", Pattern.MULTILINE);
039
040    @Override
041    public File serialize(CoreSession session, DocumentModel docfrom, String fsPath) throws IOException {
042        File folder = null;
043        File newFolder = null;
044        folder = new File(fsPath);
045
046        // if target directory doesn't exist, create it
047        if (!folder.exists()) {
048            folder.mkdir();
049        }
050
051        if ("/".equals(docfrom.getPathAsString())) {
052            // we do not serialize the root document
053            return folder;
054        }
055
056        if (docfrom.isFolder()) {
057            String fileName = encodeFilename(StringUtils.isNotBlank(docfrom.getTitle()) ? docfrom.getTitle() : docfrom.getName());
058            newFolder = avoidingCollision(new File(fsPath, fileName));
059
060            newFolder.mkdir();
061        }
062
063        // get all the blobs of the blob holder
064        BlobHolder myblobholder = docfrom.getAdapter(BlobHolder.class);
065        if (myblobholder != null && myblobholder.getBlob() != null) {
066
067            Blob blob = myblobholder.getBlob();
068            String filename = blob.getFilename();
069            File target = avoidingCollision(new File(folder, filename));
070            blob.transferTo(target);
071        }
072
073        if (newFolder != null) {
074            folder = newFolder;
075        }
076        return folder;
077    }
078
079    /**
080     * Given a file one wants to create, returns a file which name doesn't collide with the already existing files. For
081     * files, the anti-collide index is added before the extension.
082     *
083     * @param file The file to create
084     * @return a file that can be created.
085     * @since 10.3
086     */
087    protected File avoidingCollision(File file) {
088        int i = 1;
089        while (file.exists()) {
090            // If there is an extension
091            String name = file.getName();
092            if (file.isFile() && name.indexOf(".") > 0) {
093                String namePart = name.substring(0, name.indexOf("."));
094                String extPart = name.substring(name.indexOf("."));
095
096                file = new File(file.getParent(), namePart + "_" + i++ + extPart);
097            } else {
098                file = new File(file.getAbsolutePath() + "_" + i++);
099            }
100        }
101        return file;
102    }
103
104
105    protected String encodeFilename(String filename) {
106        return FORBIDDEN_CHAR_PATTERN.matcher(filename).replaceAll("-");
107    }
108
109}