package drive.api;

import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.FileList;
import com.google.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * File Service - Retrieve files from the api
 *
 * David Maignan <davidmaignan@gmail.com>
 */
public class FileService {
    private DriveService driveService;

    public FileService(){}

    @Inject
    public FileService(DriveService driveService) {
        this.driveService = driveService;
    }

    /**
     * Get all the files on the api
     *
     * @return List of files
     * @throws IOException
     */
    public List<File> getAll() throws IOException{
        List<File> result = new ArrayList<>();

        Drive.Files.List request = driveService.getDrive().files().list().setMaxResults(1000);

        do {
            try {
                FileList files = request.execute();

                result.addAll(files.getItems());
                request.setPageToken(files.getNextPageToken());

            } catch (IOException e) {
                Logger logger = LoggerFactory.getLogger(this.getClass().getName());
                logger.error("Error: e %s", e.toString());
                request.setPageToken(null);
            }
        } while (request.getPageToken() != null && request.getPageToken().length() > 0);

        return result;
    }

    /**
     * Get file by id
     * @param fileId
     * @return File|null
     */
    public File getFile(String fileId) {
        try {
            return driveService.getDrive().files().get(fileId).execute();
        } catch (IOException e) {
            Logger logger = LoggerFactory.getLogger(this.getClass().getName());
            logger.error("Error: Cannot retrieve the file: " + fileId);
        }
        return null;
    }
}

@@ -278,16 +278,44 @@
 eption;%0A
+import java.io.InputStream;%0A
 import j
@@ -484,16 +484,111 @@
 rvice %7B%0A
+    private static Logger logger = LoggerFactory.getLogger(FileService.class.getSimpleName());%0A
     priv
@@ -970,24 +970,148 @@
 yList%3C%3E();%0A%0A
+        String fields = %22files(createdTime,explicitlyTrashed,id,mimeType,modifiedTime,name,parents,size,trashed,version)%22;%0A%0A
         Driv
@@ -1179,23 +1179,21 @@
 .set
-MaxResults(1000
+Fields(fields
 );%0A%0A
@@ -1319,12 +1319,12 @@
 .get
-Item
+File
 s())
@@ -1741,32 +1741,135 @@
  result;%0A    %7D%0A%0A
+//    files(createdTime,explicitlyTrashed,id,mimeType,modifiedTime,name,parents,size,trashed,version)%0A%0A
     /**%0A     * G
@@ -2103,87 +2103,8 @@
 ) %7B%0A
-            Logger logger = LoggerFactory.getLogger(this.getClass().getName());
 %0A   
@@ -2208,11 +2208,768 @@
 l;%0A    %7D
+%0A%0A    /**%0A     * Get InputStream for a fileId%0A     * @param id%0A     * @return%0A     * @throws IOException%0A     */%0A    public InputStream downloadFile(String id) throws IOException %7B%0A        try %7B%0A            return driveService.getDrive().files().get(id).executeMediaAsInputStream();%0A        %7D catch (IOException e) %7B%0A            logger.error(%22Cannot get file from google drive api: %22 + id);%0A            return null;%0A        %7D%0A    %7D%0A%0A    /**%0A     * Get root id%0A     *%0A     * @return File root of the drive.%0A     */%0A    public File getRoot()%7B%0A        try%7B%0A            return driveService.getDrive().files().get(%22root%22).execute();%0A        %7D catch (IOException exception) %7B%0A            logger.error(exception.getMessage());%0A        %7D%0A%0A        return null;%0A    %7D
 %0A%7D%0A

Move download file to FileService