@@ -20,6 +20,7 @@ const util = require('util');
2020const requestFactory = require ( '../lib/requestwrapper' ) ;
2121const BaseService = require ( '../lib/base_service' ) ;
2222const pick = require ( 'object.pick' ) ;
23+ const isStream = require ( 'isstream' ) ;
2324
2425/**
2526 *
@@ -443,15 +444,46 @@ DiscoveryV1.prototype.getCollectionFields = function(params, callback) {
443444 return requestFactory ( parameters , callback ) ;
444445} ;
445446
447+ /**
448+ * Ensures a filename is always set because discovery ignores documents that do not have a filename set.
449+ * @param file
450+ * @private
451+ */
452+ DiscoveryV1 . _ensureFilename = function ( file ) {
453+ // no changes needed for streams created by fs.ReadStream (or similar looking streams)
454+ if ( isStream . isReadable ( file ) && file . path ) {
455+ return file ;
456+ }
457+
458+ // next handle request-style value/options objects
459+ if ( file && file . hasOwnProperty ( 'value' ) && file . hasOwnProperty ( 'options' ) ) {
460+ if ( file . options . filename ) {
461+ return file ;
462+ }
463+ return {
464+ value : file . value ,
465+ options : Object . assign ( { filename : '_' } , file . options )
466+ } ;
467+ }
468+
469+ // finally, handle all other cases by wrapping them in a request-style value/options object
470+ return {
471+ value : file ,
472+ options : {
473+ filename : '_'
474+ }
475+ } ;
476+ } ;
477+
446478/**
447479 * Add a document to a collection
448- * @param params
480+ * @param { object } params
449481 * @param {String } params.environment_id environment guid for the collection
450482 * @param {string } params.collection_id the guid of the collection to delete
451- * @param {Buffer|ReadableStream|Object } params.file a file to post (smaller than 50mb)
483+ * @param {Buffer|ReadableStream|Object } params.file a file to post (smaller than 50mb). In some cases you will need to supply an object with {value: data, options: {contentType: /...'}}
452484 * @param {string } [params.configuration_id] config guid
453- * @param {object|string } [params.metadata] JSON object with file metadata including content-type (will infer if missing)
454- * @param callback
485+ * @param {object } [params.metadata] JSON object with file metadata
486+ * @param { function } callback
455487 * @return {ReadableStream|undefined }
456488 */
457489DiscoveryV1 . prototype . addDocument = function ( params , callback ) {
@@ -460,18 +492,9 @@ DiscoveryV1.prototype.addDocument = function(params, callback) {
460492 const queryParams = pick ( params , [ 'configuration_id' ] ) ;
461493 const formDataParams = pick ( params , [ 'file' , 'metadata' ] ) ;
462494
463- // if we get a buffer or object, we need to include stuff about filename for the service
495+ // Discovery only accepts files that have a filename specified
464496 if ( formDataParams . file ) {
465- if (
466- typeof formDataParams . file . filename !== 'string' &&
467- ! ( formDataParams . file . options && typeof formDataParams . file . options . filename !== 'string' ) &&
468- ! ( formDataParams . file . path && typeof formDataParams . file . path !== 'string' ) &&
469- ! ( formDataParams . file . name && typeof formDataParams . file . name !== 'string' )
470- ) {
471- const filedat = formDataParams . file ;
472- // the filename used below is because the name must exist
473- formDataParams . file = { value : filedat , options : { filename : '_' } } ;
474- }
497+ formDataParams . file = DiscoveryV1 . _ensureFilename ( formDataParams . file ) ;
475498 }
476499
477500 if ( formDataParams . metadata && typeof formDataParams . metadata === 'object' ) {
@@ -487,12 +510,35 @@ DiscoveryV1.prototype.addDocument = function(params, callback) {
487510 formData : formDataParams ,
488511 json : true
489512 } ,
490- requiredParams : [ 'environment_id' , 'collection_id' , 'file' ] ,
513+ requiredParams : [ 'environment_id' , 'collection_id' ] ,
491514 defaultOptions : this . _options
492515 } ;
493516 return requestFactory ( parameters , callback ) ;
494517} ;
495518
519+ /**
520+ * Helper method, similar to addDocument except that the file param expects a JSON object
521+ * @param {object } params
522+ * @param {String } params.environment_id environment guid for the collection
523+ * @param {string } params.collection_id the guid of the collection to delete
524+ * @param {Object } params.file non-stringified JSON object
525+ * @param {string } [params.configuration_id] config guid
526+ * @param {object } [params.metadata] JSON object with file metadata
527+ * @param {function } callback
528+ * @return {ReadableStream|undefined }
529+ */
530+ DiscoveryV1 . prototype . addJsonDocument = function ( params , callback ) {
531+ params = Object . assign ( { } , params , {
532+ file : {
533+ value : JSON . stringify ( params . file ) ,
534+ options : {
535+ filename : '_.json'
536+ }
537+ }
538+ } ) ;
539+ return this . addDocument ( params , callback ) ;
540+ } ;
541+
496542/**
497543 * Update or partially update a document to create or replace an existing document
498544 * @param params
@@ -501,7 +547,7 @@ DiscoveryV1.prototype.addDocument = function(params, callback) {
501547 * @param {string } params.document_id the guid of the document to update
502548 * @param {Buffer|ReadableStream|Object } params.file a file to post (smaller than 50mb)
503549 * @param {string } [params.configuration_id] config guid
504- * @param {string } [params.metadata] file metadata, including content-type (will infer if missing)
550+ * @param {object } [params.metadata] file metadata, including content-type (will infer if missing)
505551 * @param callback
506552 * @return {ReadableStream|undefined }
507553 */
@@ -511,18 +557,13 @@ DiscoveryV1.prototype.updateDocument = function(params, callback) {
511557 const queryParams = pick ( params , [ 'configuration_id' ] ) ;
512558 const formDataParams = pick ( params , [ 'file' , 'metadata' ] ) ;
513559
514- // if we get a buffer or object, we need to include stuff about filename for the service
560+ // Discovery only accepts files that have a filename specified
515561 if ( formDataParams . file ) {
516- if (
517- typeof formDataParams . file . filename !== 'string' &&
518- ! ( formDataParams . file . options && typeof formDataParams . file . options . filename !== 'string' ) &&
519- ! ( formDataParams . file . path && typeof formDataParams . file . path !== 'string' ) &&
520- ! ( formDataParams . file . name && typeof formDataParams . file . name !== 'string' )
521- ) {
522- const filedat = formDataParams . file ;
523- // the filename used below is because the name must exist
524- formDataParams . file = { value : filedat , options : { filename : '_' } } ;
525- }
562+ formDataParams . file = DiscoveryV1 . _ensureFilename ( formDataParams . file ) ;
563+ }
564+
565+ if ( formDataParams . metadata && typeof formDataParams . metadata === 'object' ) {
566+ formDataParams . metadata = JSON . stringify ( formDataParams . metadata ) ;
526567 }
527568
528569 const parameters = {
0 commit comments