delta-store
Version:
An API for a store with change records
136 lines • 5.05 kB
JavaScript
/**
* Created by Papa on 1/2/2016.
*/
;
var GoogleDriveModel_1 = require("./GoogleDriveModel");
var GoogleDrive = (function () {
function GoogleDrive(googleApi) {
this.googleApi = googleApi;
var SCOPES = [
'https://www.googleapis.com/auth/drive.appfolder',
'email',
'profile',
];
}
GoogleDrive.prototype.createFolder = function (name, folderId) {
var parents;
if (folderId) {
parents = [folderId];
}
var fileMetadata = {
name: name,
mimeType: GoogleDriveModel_1.MimeTypes.FOLDER,
parents: parents
};
var createDescriptor = {
resource: fileMetadata,
fields: 'id'
};
return gapi.client.drive.files.create(createDescriptor);
};
GoogleDrive.prototype.createFile = function (name, mimeType, folderId) {
var fileMetadata = {
mimeType: mimeType,
name: name,
parents: [folderId]
};
return Promise.resolve().then(function () {
return gapi.client.drive.files.create({
resource: fileMetadata,
fields: 'id'
});
});
};
GoogleDrive.prototype.findOrCreateBook = function (name, folderId) {
return this.findOrCreateUniqueFile(name, GoogleDriveModel_1.MimeTypes.SPREAD_SHEET_BOOK, folderId);
};
GoogleDrive.prototype.findOrCreateUniqueFolder = function (fileName, folderId) {
var _this = this;
return this.findFile(fileName, folderId).then(function (response) {
var files = response.result.files;
switch (files.length) {
case 0:
return _this.createFolder(fileName, folderId);
case 1:
return {
result: {
id: files[0].id
}
};
default:
throw "Found more than one '" + fileName + "' in directory '" + folderId + "', please delete the duplicate.";
}
});
};
GoogleDrive.prototype.findOrCreateUniqueFile = function (fileName, mimeType, folderId) {
var _this = this;
return this.findFile(fileName, folderId).then(function (response) {
var files = response.result.files;
switch (files.length) {
case 0:
return _this.createFile(fileName, mimeType, folderId);
case 1:
return {
body: undefined,
headers: undefined,
result: {
id: files[0].id
},
status: undefined,
statusText: undefined
};
default:
throw "Found more than one '" + fileName + "' in directory '" + folderId + "', please delete the duplicate.";
}
});
};
GoogleDrive.prototype.apiFileList = function (dirRef) {
return new Promise(function (resolve, reject) {
resolve();
}).then(function () {
return gapi.client.drive.files.list(dirRef);
});
};
GoogleDrive.prototype.findFile = function (fileName, folderId) {
if (folderId === void 0) { folderId = GoogleDriveModel_1.DriveConstants.DRIVE_FOLDER; }
var query = "name = '" + fileName + "' and '" + folderId + "' in parents and trashed=false";
return this.apiFileList({
q: query
}).then(function (response) {
console.log('Found for q:\n\t' + query);
console.log(response);
return response;
}).catch(function (error) {
console.log('Did not find for q:\n\t' + query);
if (error.status === 404) {
return {
result: {
files: []
}
};
}
throw error;
});
};
GoogleDrive.prototype.listFiles = function (folderId, pageToken, space) {
if (pageToken === void 0) { pageToken = null; }
if (space === void 0) { space = GoogleDriveModel_1.DriveConstants.DRIVE_SPACE; }
return this.apiFileList({
fields: 'nextPageToken, files(id, mimeType, name)',
pageToken: pageToken,
q: "'" + folderId + "' in parents and trashed = false",
spaces: space
});
};
GoogleDrive.prototype.searchFiles = function (space) {
if (space === void 0) { space = GoogleDriveModel_1.DriveConstants.DRIVE_SPACE; }
return this.apiFileList({
spaces: space,
fields: GoogleDriveModel_1.DriveConstants.APP_DATA_LIST_FIELDS,
pageSize: 100
});
};
return GoogleDrive;
}());
exports.GoogleDrive = GoogleDrive;
//# sourceMappingURL=GoogleDrive.js.map