/*
 * Copyright 2018 The Data Transfer Project Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.datatransferproject.datatransfer.backblaze.videos;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
import org.datatransferproject.api.launcher.Monitor;
import org.datatransferproject.datatransfer.backblaze.common.BackblazeDataTransferClient;
import org.datatransferproject.datatransfer.backblaze.common.BackblazeDataTransferClientFactory;
import org.datatransferproject.spi.cloud.storage.TemporaryPerJobDataStore;
import org.datatransferproject.spi.transfer.idempotentexecutor.IdempotentImportExecutor;
import org.datatransferproject.spi.transfer.provider.ImportResult;
import org.datatransferproject.spi.transfer.provider.Importer;
import org.datatransferproject.transfer.ImageStreamProvider;
import org.datatransferproject.types.common.models.videos.VideoModel;
import org.datatransferproject.types.common.models.videos.VideosContainerResource;
import org.datatransferproject.types.transfer.auth.TokenSecretAuthData;

public class BackblazeVideosImporter
    implements Importer<TokenSecretAuthData, VideosContainerResource> {

  private static final String VIDEO_TRANSFER_MAIN_FOLDER = "Video Transfer";

  private final TemporaryPerJobDataStore jobStore;
  private final ImageStreamProvider imageStreamProvider;
  private final Monitor monitor;
  private final BackblazeDataTransferClientFactory b2ClientFactory;

  public BackblazeVideosImporter(
          Monitor monitor,
          TemporaryPerJobDataStore jobStore,
          ImageStreamProvider imageStreamProvider,
          BackblazeDataTransferClientFactory b2ClientFactory) {
    this.monitor = monitor;
    this.jobStore = jobStore;
    this.imageStreamProvider = imageStreamProvider;
    this.b2ClientFactory = b2ClientFactory;
  }

  @Override
  public ImportResult importItem(
      UUID jobId,
      IdempotentImportExecutor idempotentExecutor,
      TokenSecretAuthData authData,
      VideosContainerResource data)
      throws Exception {
    if (data == null) {
      // Nothing to do
      return ImportResult.OK;
    }

    BackblazeDataTransferClient b2Client = b2ClientFactory.getOrCreateB2Client(jobId, authData);

    if (data.getVideos() != null && data.getVideos().size() > 0) {
      for (VideoModel video : data.getVideos()) {
        idempotentExecutor.executeAndSwallowIOExceptions(
            video.getDataId(), video.getName(), () -> importSingleVideo(b2Client, video));
      }
    }

    return ImportResult.OK;
  }

  private String importSingleVideo(BackblazeDataTransferClient b2Client, VideoModel video)
      throws IOException {
    InputStream videoFileStream =
        imageStreamProvider.getConnection(video.getContentUrl().toString()).getInputStream();

    return b2Client.uploadFile(
        String.format("%s/%s.mp4", VIDEO_TRANSFER_MAIN_FOLDER, video.getDataId()),
        jobStore.getTempFileFromInputStream(videoFileStream, video.getDataId(), ".mp4"));
  }
}

@@ -673,16 +673,54 @@
 ideos;%0A%0A
+import java.io.FileNotFoundException;%0A
 import j
@@ -3219,16 +3219,21 @@
 n %7B%0A    
+try (
 InputStr
@@ -3346,19 +3346,23 @@
 Stream()
-;%0A%0A
+) %7B%0A%0A  
     retu
@@ -3385,16 +3385,18 @@
 adFile(%0A
+  
         
@@ -3478,16 +3478,18 @@
         
+  
 jobStore
@@ -3562,14 +3562,194 @@
 mp4%22));%0A
+    %7D catch (FileNotFoundException e) %7B%0A      monitor.severe(%0A          () -%3E String.format(%22Video resource was missing for id: %25s%22, video.getDataId()), e);%0A    %7D%0A    return null;%0A
   %7D%0A%7D%0A

Handle Backblaze FileNotFoundException for videos (#1040)