Changeset View
Changeset View
Standalone View
Standalone View
lib/app/base-chips/field-types/file.js
"use strict"; | |||||
const locreq = require("locreq")(__dirname); | const locreq = require("locreq")(__dirname); | ||||
const Promise = require("bluebird"); | const Promise = require("bluebird"); | ||||
const File = locreq("lib/data-structures/file.js"); | const File = locreq("lib/data-structures/file.js"); | ||||
const axios = require("axios"); | const axios = require("axios"); | ||||
const url = require("url"); | const url = require("url"); | ||||
module.exports = function(app) { | module.exports = function(app) { | ||||
return { | return { | ||||
name: "file", | name: "file", | ||||
handles_large_data: true, | handles_large_data: true, | ||||
is_proper_value: function(context, params, value) { | is_proper_value(context, params, value) { | ||||
if (value === undefined) { | if (value === undefined) { | ||||
return undefined; | return undefined; | ||||
} | } | ||||
if (typeof value == "string") { | if (typeof value === "string") { | ||||
return axios(value) | return axios(value) | ||||
.then(function() { | .then(() => Promise.resolve()) | ||||
return Promise.resolve(); | .catch(() => | ||||
}) | Promise.reject( | ||||
.catch(function() { | |||||
return Promise.reject( | |||||
`There was a problem while getting the file: '${value}'. Is the URL correct?` | `There was a problem while getting the file: '${value}'. Is the URL correct?` | ||||
) | |||||
); | ); | ||||
}); | |||||
} | } | ||||
if ( | if ( | ||||
value === null || | value === null || | ||||
value === "" || | value === "" || | ||||
value instanceof File || | value instanceof File || | ||||
(value.filename !== undefined && value.data instanceof Buffer) | (value.filename !== undefined && value.data instanceof Buffer) | ||||
) { | ) { | ||||
return Promise.resolve(); | return Promise.resolve(); | ||||
} else { | } | ||||
let type; | let type; | ||||
if (value instanceof Array) { | if (value instanceof Array) { | ||||
type = | type = | ||||
"<Array>. If you want to upload multiple files, use array field types."; | "<Array>. If you want to upload multiple files, use array field types."; | ||||
} else { | } else { | ||||
type = typeof data; | type = typeof data; | ||||
} | } | ||||
return Promise.reject( | return Promise.reject( | ||||
`Wrong file data format. Should be Sealious.File, but received ${type}` | `Wrong file data format. Should be Sealious.File, but received ${type}` | ||||
); | ); | ||||
} | |||||
}, | }, | ||||
encode: function(context, params, value_in_code) { | encode(context, params, value_in_code) { | ||||
if (typeof value_in_code === "string") { | if (typeof value_in_code === "string") { | ||||
return axios(value_in_code, { | return axios(value_in_code, { | ||||
responseType: "arrayBuffer", | responseType: "arrayBuffer", | ||||
}).then(function(file_buffer) { | }).then(file_buffer => { | ||||
const filename = url | const filename = url | ||||
.parse(value_in_code) | .parse(value_in_code) | ||||
.pathname.split("/") | .pathname.split("/") | ||||
.pop(); | .pop(); | ||||
const file = new File(context, filename, file_buffer); | const file = new File(context, filename, file_buffer); | ||||
return app.FileManager.save_file(file); | return app.FileManager.save_file(file); | ||||
}); | }); | ||||
} | } | ||||
if (value_in_code) { | if (value_in_code) { | ||||
return app.FileManager.save_file(value_in_code); | return app.FileManager.save_file(value_in_code); | ||||
} else { | |||||
return null; | |||||
} | } | ||||
return null; | |||||
}, | }, | ||||
decode: function(context, params, value_in_database) { | decode(context, params, value_in_database) { | ||||
if (value_in_database) { | if (value_in_database) { | ||||
return Promise.resolve( | return Promise.resolve( | ||||
new File.Reference( | new File.Reference( | ||||
value_in_database.id, | value_in_database.id, | ||||
value_in_database.filename | value_in_database.filename | ||||
) | ) | ||||
); | ); | ||||
} else if (params.no_file_value) { | } else if (params.no_file_value) { | ||||
return params.no_file_value; | return params.no_file_value; | ||||
} else { | |||||
return undefined; | |||||
} | } | ||||
return undefined; | |||||
}, | }, | ||||
format: function(context, params, decoded_value, format) { | format(context, params, decoded_value, format) { | ||||
if (decoded_value === undefined) { | if (decoded_value === undefined) { | ||||
return undefined; | return undefined; | ||||
} | } | ||||
if (format === "internal") return decoded_value; | if (format === "internal") return decoded_value; | ||||
if (format === "url" || format === undefined) { | if (format === "url" || format === undefined) { | ||||
return `/api/v1/uploaded-files/${decoded_value.id}/${ | return `/api/v1/uploaded-files/${decoded_value.id}/${ | ||||
decoded_value.filename | decoded_value.filename | ||||
}`; | }`; | ||||
} else if (format === "url-with-filename") { | } else if (format === "url-with-filename") { | ||||
if (decoded_value === undefined) { | if (decoded_value === undefined) { | ||||
return { | return { | ||||
url: params.no_file_value || "", | url: params.no_file_value || "", | ||||
filename: null, | filename: null, | ||||
empty: true, | empty: true, | ||||
}; | }; | ||||
} else { | } | ||||
return { | return { | ||||
url: `/api/v1/uploaded-files/${decoded_value.id}/${ | url: `/api/v1/uploaded-files/${decoded_value.id}/${ | ||||
decoded_value.filename | decoded_value.filename | ||||
}`, | }`, | ||||
filename: decoded_value.filename, | filename: decoded_value.filename, | ||||
empty: false, | empty: false, | ||||
}; | }; | ||||
} | } | ||||
} else { | |||||
return decoded_value; | return decoded_value; | ||||
} | |||||
}, | }, | ||||
}; | }; | ||||
}; | }; |