Skip to content

Commit

Permalink
feat: auto_transcription on upload and explicit support (#690)
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudinary-pkoniu authored Sep 13, 2024
1 parent 7deaf1b commit 94d21ab
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,10 @@ function build_upload_params(options) {
use_asset_folder_as_public_id_prefix: utils.as_safe_bool(options.use_asset_folder_as_public_id_prefix),
visual_search: utils.as_safe_bool(options.visual_search),
on_success: options.on_success,
auto_transcription: options.auto_transcription,
auto_chaptering: utils.as_safe_bool(options.auto_chaptering)
};

return utils.updateable_resource_params(options, params);
}

Expand Down Expand Up @@ -725,6 +727,17 @@ function updateable_resource_params(options, params = {}) {
if (options.regions != null) {
params.regions = JSON.stringify(options.regions);
}
const autoTranscription = options.auto_transcription;
if (autoTranscription != null) {
if (typeof autoTranscription === 'boolean') {
params.auto_transcription = utils.as_safe_bool(autoTranscription);
} else {
const isAutoTranscriptionObject = typeof autoTranscription === 'object' && !Array.isArray(autoTranscription);
if (isAutoTranscriptionObject && Object.keys(autoTranscription).includes('translate')) {
params.auto_transcription = JSON.stringify(autoTranscription);
}
}
}
return params;
}

Expand Down
43 changes: 43 additions & 0 deletions test/integration/api/uploader/auto_transcription_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const sinon = require('sinon');
const cloudinary = require('../../../../lib/cloudinary');
const helper = require('../../../spechelper');
const ClientRequest = require('_http_client').ClientRequest;

describe('Uploader', () => {
let spy;
let xhr;

before(() => {
xhr = sinon.useFakeXMLHttpRequest();
spy = sinon.spy(ClientRequest.prototype, 'write');
});

after(() => {
spy.restore();
xhr.restore();
});

describe('upload', () => {
it('should send a request with auto_transcription set to true if requested', () => {
cloudinary.v2.uploader.upload('irrelevant', { auto_transcription: true });
sinon.assert.calledWith(spy, sinon.match(helper.uploadParamMatcher('auto_transcription', '1')));
});

it('should send a request with auto_transcription config if requested', () => {
cloudinary.v2.uploader.upload('irrelevant', { auto_transcription: { translate: ['pl'] } });
sinon.assert.calledWith(spy, sinon.match(helper.uploadParamMatcher('auto_transcription', '{"translate":["pl"]}')));
});
});

describe('explicit', () => {
it('should send a request with auto_transcription set to true if requested', () => {
cloudinary.v2.uploader.explicit('irrelevant', { auto_transcription: true });
sinon.assert.calledWith(spy, sinon.match(helper.uploadParamMatcher('auto_transcription', '1')));
});

it('should send a request with auto_transcription config if requested', () => {
cloudinary.v2.uploader.explicit('irrelevant', { auto_transcription: { translate: ['pl'] } });
sinon.assert.calledWith(spy, sinon.match(helper.uploadParamMatcher('auto_transcription', '{"translate":["pl"]}')));
});
});
});
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ declare module 'cloudinary' {
use_asset_folder_as_public_id_prefix?: boolean;
regions?: Record<string, [RegionCoordinate, RegionCoordinate, ...Array<RegionCoordinate>]>;
auto_chaptering?: boolean;
auto_transcription?: boolean | { translate: Array<string>; };

[futureKey: string]: any;
}
Expand Down

0 comments on commit 94d21ab

Please sign in to comment.