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S,IAAI,MAAM,GAAI,GACtC,EAAS,KAAO,IAET,CACX,IACC,OAAM,WAEH,OADA,QAAQ,IAAI,kGACL,EAAK,OAAO,uBAAuB,CAAC,KAAM,IAC5C,MAAK,SAAU,GAKZ,OAHA,EAAS,MAAM,GAAG,IAAM,EAAS,MAAM,GAAG,IAAI,MAAM,GAAI,GACxD,EAAS,MAAM,GAAG,KAAO,IACzB,QAAQ,IAAI,0BACL,EAAS,MAAM,EAC1B,GACR,GACR,EACA,OAAO,QAAU","file":"/npm/keystone-storage-adapter-dropbox@1.0.1/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/keystone-storage-adapter-dropbox@1.0.1/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","/*\r\nTODO\r\n- Check whether files exist before uploading (will always overwrite as-is)\r\n- Support multiple retry attempts if a file exists (see FS Adapter)\r\n*/\r\n\r\n// Mirroring keystone 0.4's support of node 0.12.\r\nvar assign = require('object-assign');\r\nvar ensureCallback = require('keystone-storage-namefunctions/ensureCallback');\r\n\r\nvar Dropbox = require('dropbox');\r\nvar nameFunctions = require('keystone-storage-namefunctions');\r\nvar pathlib = require('path');\r\nvar fs = require('fs');\r\n\r\nvar DEFAULT_OPTIONS = {\r\n    accessToken: process.env.DROPBOX_ACCESS_TOKEN,\r\n    generateFilename: nameFunctions.randomFilename,\r\n};\r\n\r\n// This constructor is usually called indirectly by the Storage class\r\n// in keystone.\r\n\r\n// S3-specific options should be specified in an `options.s3` field,\r\n// which can contain the following options: { key, secret, bucket, region,\r\n// headers, path }.\r\n\r\n// The schema can contain the additional fields { path, bucket, etag }.\r\n\r\n// See README.md for details and usage examples.\r\n\r\nfunction DropboxAdapter(options, schema) {\r\n    this.options = assign({}, DEFAULT_OPTIONS, options.dropbox);\r\n\r\n    // Support `defaultHeaders` option alias for `headers`\r\n    // TODO: Remove me with the next major version bump\r\n    // if (this.options.defaultHeaders) {\r\n    //     this.options.headers = this.options.defaultHeaders;\r\n    // }\r\n\r\n    // Knox will check for the 'key', 'secret' and 'bucket' options.\r\n    this.client = new Dropbox(this.options);\r\n    // this.client = knox.createClient(this.options);\r\n\r\n    // If path is specified it must be absolute.\r\n    if (options.path !== null && !pathlib.isAbsolute(options.path)) {\r\n        throw Error('Configuration error: Dropbox path must be absolute');\r\n    }\r\n\r\n    // Ensure the generateFilename option takes a callback\r\n    this.options.generateFilename = ensureCallback(this.options.generateFilename);\r\n}\r\n\r\nDropboxAdapter.compatibilityLevel = 1;\r\n\r\n// All the extra schema fields supported by this adapter.\r\nDropboxAdapter.SCHEMA_TYPES = {\r\n    filename: String,\r\n    path_display: String,\r\n    path: String,\r\n    id: String,\r\n    url: String,\r\n    originalname: String,   // the original (uploaded) name of the file; useful when filename generated\r\n};\r\n\r\nDropboxAdapter.SCHEMA_FIELD_DEFAULTS = {\r\n    filename: true,\r\n    path_display: true,\r\n    path: true,\r\n    id: true,\r\n    url: true,\r\n    originalname: true,   // the original (uploaded) name of the file; useful when filename generated\r\n};\r\n\r\n// Get the full, absolute path name for the specified file.\r\nDropboxAdapter.prototype._resolveFilename = function (file) {\r\n    // Just like the bucket, the schema can store the path for files. If the path\r\n    // isn't stored we'll assume all the files are in the path specified in the\r\n    // s3.path option. If that doesn't exist we'll assume the file is in the root\r\n    // of the bucket. (Whew!)\r\n    var path = file.path || this.options.path || '/';\r\n    return pathlib.posix.resolve(path, file.filename);\r\n};\r\n\r\nDropboxAdapter.prototype.uploadFile = function (file, callback) {\r\n    var self = this;\r\n    this.options.generateFilename(file, 0, function (err, filename) {\r\n        if (err) return callback(err);\r\n\r\n        // The expanded path of the file on the filesystem.\r\n        var localpath = file.path;\r\n        console.log('file saved to', localpath);\r\n\r\n        // The destination path inside the S3 bucket.\r\n        file.path = self.options.path;\r\n        file.filename = file.originalname;\r\n        var destpath = self._resolveFilename(file);\r\n\r\n        // Figure out headers\r\n        var headers = assign({}, self.options.headers, {\r\n            'Content-Length': file.size,\r\n            'Content-Type': file.mimetype\r\n        });\r\n        console.log('file headers', headers);\r\n\r\n        fs.readFile(localpath, function (err, data) {\r\n            if (err) return callback(err);\r\n\r\n            self.client.filesUpload({path: destpath, contents: data})\r\n                .then(function (response) {\r\n                    return self.share(response.path_display, response.id)\r\n                })\r\n                .then(function (shared_file) {\r\n                    return callback(null, assign({}, shared_file, file));\r\n                })\r\n                .catch(function (error) {\r\n                    callback(error);\r\n                });\r\n        });\r\n    });\r\n};\r\n\r\n// Note that this will provide a public URL for the file, but it will only\r\n// work if:\r\n// - the bucket is public (best) or\r\n// - the file is set to a canned ACL (ie, headers:{ 'x-amz-acl': 'public-read' } )\r\n// - you pass credentials during your request for the file content itself\r\nDropboxAdapter.prototype.getFileURL = function (file) {\r\n    // Consider providing an option to use insecure http. I can't think of any\r\n    // sensible use case for plain http though. https should be used everywhere.\r\n    return file.url;\r\n};\r\n\r\nDropboxAdapter.prototype.removeFile = function (file, callback) {\r\n    var fullpath = this._resolveFilename(file);\r\n\r\n    this.client.filesDeleteV2({\r\n        path: fullpath\r\n    })\r\n        .then(function () {\r\n            callback();\r\n        })\r\n        .catch(function (e) {\r\n            callback(e);\r\n        })\r\n};\r\n\r\n// Check if a file with the specified filename already exists. Callback called\r\n// with the file headers if the file exists, null otherwise.\r\nDropboxAdapter.prototype.fileExists = function (filename, callback) {\r\n    var fullpath = this._resolveFilename({filename: filename});\r\n    this.client.filesListFolder({path: fullpath})\r\n        .then(function (res) {\r\n            if (res.entries.length) {\r\n                return callback(null, res.headers);\r\n            }\r\n            return callback();\r\n        })\r\n        .catch(function (err) {\r\n            if (err) return callback(err);\r\n\r\n        })\r\n};\r\n\r\nDropboxAdapter.prototype.fileList = function (path) {// '/example_folder'\r\n    return this.client.filesListFolder({path: path})\r\n\r\n}\r\n\r\nDropboxAdapter.prototype.updateFolder = function (folder_name) {\r\n    var self = this;\r\n    return this.client.filesDeleteV2({\r\n        path: '/' + folder_name\r\n    })\r\n        .catch(function (e) {\r\n            return self.client.filesCreateFolderV2({path: '/' + folder_name})\r\n        })\r\n};\r\n\r\nDropboxAdapter.prototype.share = function (path_display, id) {\r\n    var shareRes;\r\n    var self = this;\r\n\r\n    return this.client.sharingCreateSharedLinkWithSettings({path: path_display})\r\n        .then(function () {\r\n            //do this for direct access (...dl=0 => ...dl=1)\r\n            shareRes.url = shareRes.url.slice(0, -1);\r\n            shareRes.url += '1';\r\n\r\n            return shareRes;\r\n        })\r\n        .catch(function () {\r\n            console.log('cannot share by sharingCreateSharedLinkWithSettings, trying to share by sharingListSharedLinks');\r\n            return self.client.sharingListSharedLinks({path: id})\r\n                .then(function (response) {\r\n                    //do this for direct access (...dl=0 => ...dl=1)\r\n                    response.links[0].url = response.links[0].url.slice(0, -1);\r\n                    response.links[0].url += '1';\r\n                    console.log('successful file upload');\r\n                    return response.links[0];\r\n                });\r\n        })\r\n};\r\nmodule.exports = DropboxAdapter;\r\n"]}