001/*
002 * (C) Copyright 2016 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 *     Kevin Leturc
018 */
019package org.nuxeo.ecm.liveconnect.onedrive;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.net.URI;
024import java.util.Collections;
025import java.util.List;
026
027import javax.servlet.http.HttpServletRequest;
028
029import org.nuxeo.ecm.core.blob.BlobManager.UsageHint;
030import org.nuxeo.ecm.core.blob.ManagedBlob;
031import org.nuxeo.ecm.core.blob.apps.AppLink;
032import org.nuxeo.ecm.liveconnect.core.AbstractLiveConnectBlobProvider;
033import org.nuxeo.ecm.liveconnect.core.LiveConnectFile;
034import org.nuxeo.ecm.liveconnect.core.LiveConnectFileInfo;
035import org.nuxeo.onedrive.client.OneDriveAPI;
036import org.nuxeo.onedrive.client.OneDriveFile;
037import org.nuxeo.onedrive.client.OneDriveSharingLink.Type;
038import org.nuxeo.onedrive.client.OneDriveThumbnailSize;
039
040import com.google.api.client.auth.oauth2.Credential;
041
042/**
043 * @since 8.2
044 */
045public class OneDriveBlobProvider extends AbstractLiveConnectBlobProvider<OneDriveOAuth2ServiceProvider> {
046
047    private static final String CACHE_NAME = "onedrive";
048
049    private static final String ONEDRIVE_DOCUMENT_TO_BE_UPDATED_PP = "onedrive_document_to_be_updated";
050
051    @Override
052    protected String getCacheName() {
053        return CACHE_NAME;
054    }
055
056    @Override
057    protected String getPageProviderNameForUpdate() {
058        return ONEDRIVE_DOCUMENT_TO_BE_UPDATED_PP;
059    }
060
061    @Override
062    public URI getURI(ManagedBlob blob, UsageHint usage, HttpServletRequest servletRequest) throws IOException {
063        LiveConnectFileInfo fileInfo = toFileInfo(blob);
064        String url = null;
065        switch (usage) {
066        case STREAM:
067        case DOWNLOAD:
068            url = prepareOneDriveFile(fileInfo).getMetadata().getDownloadUrl();
069            break;
070        case VIEW:
071        case EDIT:
072            url = prepareOneDriveFile(fileInfo).createSharedLink(Type.EDIT).getLink().getWebUrl();
073            break;
074        }
075        return url == null ? null : asURI(url);
076    }
077
078    @Override
079    public InputStream getStream(ManagedBlob blob) throws IOException {
080        return prepareOneDriveFile(toFileInfo(blob)).download();
081    }
082
083    @Override
084    public InputStream getThumbnail(ManagedBlob blob) throws IOException {
085        return prepareOneDriveFile(toFileInfo(blob)).downloadThumbnail(OneDriveThumbnailSize.LARGE);
086    }
087
088    @Override
089    public List<AppLink> getAppLinks(String username, ManagedBlob blob) throws IOException {
090        // application links do not work with document which are not office document
091        if (!blob.getMimeType().contains("officedocument")) {
092            return Collections.emptyList();
093        }
094        // application links do not work with revisions
095        LiveConnectFileInfo fileInfo = toFileInfo(blob);
096        if (fileInfo.getRevisionId().isPresent()) {
097            return Collections.emptyList();
098        }
099        AppLink appLink = new AppLink();
100        String appUrl = prepareOneDriveFile(fileInfo).createSharedLink(Type.EDIT).getLink().getWebUrl();
101        appLink.setLink(appUrl);
102        appLink.setAppName("Microsoft OneDrive");
103        appLink.setIcon("icons/OneDrive.png");
104        return Collections.singletonList(appLink);
105    }
106
107    protected OneDriveAPI getOneDriveAPI(LiveConnectFileInfo fileInfo) throws IOException {
108        return getOneDriveAPI(getCredential(fileInfo));
109    }
110
111    protected OneDriveAPI getOneDriveAPI(Credential credential) {
112        return getOAuth2Provider().getAPIInitializer().apply(credential.getAccessToken());
113    }
114
115    @Override
116    protected LiveConnectFile retrieveFile(LiveConnectFileInfo fileInfo) throws IOException {
117        return new OneDriveLiveConnectFile(fileInfo, retrieveOneDriveFileMetadata(fileInfo));
118    }
119
120    protected OneDriveFile.Metadata retrieveOneDriveFileMetadata(LiveConnectFileInfo fileInfo) throws IOException {
121        return prepareOneDriveFile(fileInfo).getMetadata();
122    }
123
124    protected OneDriveFile prepareOneDriveFile(LiveConnectFileInfo fileInfo) throws IOException {
125        OneDriveAPI api = getOneDriveAPI(fileInfo);
126        return new OneDriveFile(api, fileInfo.getFileId());
127    }
128
129}