Как загрузить файлы из 24SevenOffice API (FileService)? - PullRequest
0 голосов
/ 20 июня 2019

Я работаю в Node.js с API 24SevenOffice уже некоторое время, последний раз с FileService. Я никогда не загружал файл из API, поэтому надеялся, что это можно сделать. В документации представлены четыре соответствующих способа загрузки файлов (PrepareFileDownload, DownloadFileChunck, HashFile, EndFileDownload).

Вам понадобится ключ API и учетная запись 24sevenoffice. Я просто ищу объяснение того, как это можно сделать:

  • Нужно ли мне использовать все четыре метода для выполнения работы?
  • Что делают методы?
  • Какие другие функции / методы и т. Д. Могут быть важны для использования, writestream / readstream?
  • Что означает ввод chunckNum? (chunckNum - это имя ввода в методе DownloadFileChunck)
  • Что мне делать с ответом от метода DownloadFileChunck. (Здесь я думаю, что мне придется использовать некоторые другие методы / функции)

Я пробовал несколько способов объединить эти четыре метода, но я не совсем понимаю, что делаю, поэтому решил обратиться за помощью. Я не думаю, что я очень много пытался с HashFile методом. Имеет смысл начинать с PrepareFileDownload и заканчивать EndFileDownload, но что мне делать между ними?

Это просто для показа методов. Не ожидая, что этот код будет работать. Очевидно, что нужно что-то делать для хранения содержимого файла или чего-то еще. Код не содержит метод HashFile.

    //24sevenoffice.js is another module that takes care of logging in with the API key, username, password and all that
    const office = require("./24sevenoffice.js");
    const fs = require("fs");
    const chuncksize = ;

    //I realize that I might be on my way to callback hell here, but I will clean this up later. This is just for showing you guys.
    office.login((err) => {
        if (err){
            console.log(err);
        }

        office.service("https://webservices.24sevenoffice.com/file/V001/FileService.asmx?wsdl", (err, FileService) => {
            if (err){
                console.log(err);
            }     
            else {
                //logging in works fine
                FileService.PrepareFileDownload({FileId: 2908751, FolderId: 1256067, CheckOut: false}, (err, bytes) => {
                    if(err){
                        console.log(err);
                    }
                    else{
                        //preparefiledownload also works fine and I'm getting the TmpId and the length of the file as response.

                        console.log(bytes);

                        //My best guess for the Chuncknum input is that it states how many chuncks I want the file to be downloaded in
                        //Then it would make sense to divide the length by some constant chunksize, I hope
                        let CNUM = Math.floor(bytes.PrepareFileDownloadResult.Length/chuncksize);
                        if(CNUM == 0){
                            CNUM = 1;
                        }

                        let TMPID = new String(bytes.PrepareFileDownloadResult.TmpId);

                        //This is how I think the EndFileDownload method should look:
                        FileService.DownloadFileChunk({TmpId: TMPID, ChunckNum: CNUM}, (err, response) => {
                            if(err){
                                console.log(err);
                            }
                            else{
                                console.dir(response);
                            }
                        });

                        //This is how I think the EndFileDownload method should look:
                        FileService.EndFileDownload({TmpId: TMPID}, (err, response) => {
                            if(err){
                                console.log(err);
                            }
                            else{
                                console.log(response);
                            }
                        });
                    }
                });
            }
        });
    });
...