001/* 002 * (C) Copyright 2017 Nuxeo (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 * Guillaume Renard <grenard@nuxeo.com> 018 */ 019package org.nuxeo.ecm.automation.core.operations.blob; 020 021import java.io.IOException; 022import java.util.Collections; 023import java.util.List; 024import java.util.Objects; 025import java.util.UUID; 026import java.util.stream.Collectors; 027 028import org.apache.commons.lang3.StringUtils; 029import org.apache.logging.log4j.LogManager; 030import org.apache.logging.log4j.Logger; 031import org.nuxeo.ecm.automation.core.Constants; 032import org.nuxeo.ecm.automation.core.annotations.Context; 033import org.nuxeo.ecm.automation.core.annotations.Operation; 034import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 035import org.nuxeo.ecm.automation.core.annotations.Param; 036import org.nuxeo.ecm.core.api.Blob; 037import org.nuxeo.ecm.core.api.CoreSession; 038import org.nuxeo.ecm.core.api.DocumentModelList; 039import org.nuxeo.ecm.core.io.download.DownloadService; 040import org.nuxeo.ecm.core.utils.BlobUtils; 041import org.nuxeo.runtime.api.Framework; 042 043/** 044 * Bulk Download Operation. 045 * 046 * @since 9.3 047 */ 048@Operation(id = BulkDownload.ID, category = Constants.CAT_BLOB, label = "Bulk Download", description = "Prepare a Zip of a list of documents.") 049public class BulkDownload { 050 051 public static final String ID = "Blob.BulkDownload"; 052 053 private static final Logger log = LogManager.getLogger(BulkDownload.class); 054 055 @Context 056 protected CoreSession session; 057 058 @Param(name = "filename", required = false) 059 protected String fileName; 060 061 @OperationMethod 062 public Blob run(DocumentModelList docs) throws IOException { 063 064 DownloadService downloadService = Framework.getService(DownloadService.class); 065 List<Blob> blobs = docs.stream().map(doc -> { 066 Blob blob = downloadService.resolveBlob(doc); 067 if (blob == null) { 068 log.trace("Not able to resolve blob"); 069 return null; 070 } 071 if (!downloadService.checkPermission(doc, null, blob, "download", Collections.emptyMap())) { 072 log.debug("Not allowed to bulk download blob for document {}", doc::getPathAsString); 073 return null; 074 } 075 return blob; 076 }).filter(Objects::nonNull).collect(Collectors.toList()); 077 078 if (blobs.isEmpty()) { 079 log.debug("No blob to be zipped"); 080 return null; 081 } 082 083 String filename = StringUtils.isNotBlank(this.fileName) ? this.fileName 084 : String.format("BlobListZip-%s-%s", UUID.randomUUID(), session.getPrincipal().getName()); 085 return BlobUtils.zip(blobs, filename); 086 } 087 088}