Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLD-48-organization-content #43

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var File = require('dw/io/File');
var Site = require('dw/system/Site');
var URLUtils = require('dw/web/URLUtils');

var WS = require('*/cartridge/scripts/service/cldWebService');
var WS = require('~/cartridge/scripts/service/cldWebService');

var assetsLimit = 0;
var changedFilesCount = 0;
Expand Down Expand Up @@ -154,8 +154,8 @@ var removeRedundantTags = function (redundantTags, existingTags) {
*/
var uploadFile = function (cloudinaryConstants, asset, tags, assignedFolder, assetPublicID, metadata, svcArgs, executionMode) {
var isAssetUploaded = false;
var cloudinarySvc = require('*/cartridge/scripts/service/cldUpload');
var cloudinaryTagsSvc = require('*/cartridge/scripts/service/cldAddAssetTags');
var cloudinarySvc = require('~/cartridge/scripts/service/cldUpload');
var cloudinaryTagsSvc = require('~/cartridge/scripts/service/cldAddAssetTags');

var args = svcArgs.svcConfigArgs;
var uploadResult;
Expand All @@ -178,7 +178,7 @@ var uploadFile = function (cloudinaryConstants, asset, tags, assignedFolder, ass
if (uploadResult.errorCode === cloudinaryConstants.ERROR_CODES.UNAUTHORIZED) {
jobLogger.error('Error occurred while connecting with service due to invalid credentials, message: {0}', uploadResult.message);
} else {
jobLogger.error('Error uploading file: {0}, message: {1}', asset, uploadResult.message);
jobLogger.error('jobStepHelpers~uploadFile: Error uploading file: {0}, message: {1}, line number 177', asset, uploadResult.message);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asad-rafter Let's revert this line for log message consistency

}
} else if ('resultObj' in uploadResult && !empty(uploadResult.resultObj) && 'existing' in uploadResult.resultObj &&
uploadResult.resultObj.existing) {
Expand Down Expand Up @@ -206,7 +206,7 @@ var uploadFile = function (cloudinaryConstants, asset, tags, assignedFolder, ass
* @returns {string} asset rel URL
*/
var getAssetRelURL = function (assetURL, includeVideoExtension) {
var cloudinaryConstants = require('*/cartridge/scripts/util/cloudinaryConstants');
var cloudinaryConstants = require('~/cartridge/scripts/util/cloudinaryConstants');

var endToken;
var relURL = '';
Expand Down Expand Up @@ -269,8 +269,8 @@ var logAssetLargerThanLimitMsg = function (filePath, fileSize) {
* @returns {Object} - object holding public id and folder
*/
var changePublicIdAndCloudFolder = function (cldAssetPublicID, cldAssetFolder, isProductAsset) {
var cloudinaryUtils = require('*/cartridge/scripts/util/cloudinaryUtils');
var cloudinaryConstants = require('*/cartridge/scripts/util/cloudinaryConstants');
var cloudinaryUtils = require('~/cartridge/scripts/util/cloudinaryUtils');
var cloudinaryConstants = require('~/cartridge/scripts/util/cloudinaryConstants');

var assetPublicID = cldAssetPublicID;
var cldFolder = cldAssetFolder;
Expand Down Expand Up @@ -324,8 +324,8 @@ var logAssetPathChangedMessage = function (originalFilePath, changedFilePath) {
* @returns {Object} svcArgs - data related to service
*/
var getCldUploadSvcArgs = function () {
var cloudinaryConstants = require('*/cartridge/scripts/util/cloudinaryConstants');
var cloudinaryUtils = require('*/cartridge/scripts/util/cloudinaryUtils');
var cloudinaryConstants = require('~/cartridge/scripts/util/cloudinaryConstants');
var cloudinaryUtils = require('~/cartridge/scripts/util/cloudinaryUtils');
var svcConfigArgs = {};
var servicePrefs = cloudinaryUtils.buildUploadServicePrefs(cloudinaryConstants);
var service = WS.getService(servicePrefs.CLD_UPLOAD_SVC, WS.getServiceConfigs(svcConfigArgs));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
'use strict';

/**
* Creates valid paath argument and uploads a single asset
*
* @param {string} folder - current processing folder
* @param {Object} file - current file
* @returns {boolean} - true/false
*/
function doFile(folder, file) {
var cloudinarySvc = require('~/cartridge/scripts/service/cldUpload');
var cloudinaryConstants = require('~/cartridge/scripts/util/cloudinaryConstants');
var jobStepHelpers = require('~/cartridge/scripts/helpers/jobStepHelpers');
var cloudinaryUtils = require('~/cartridge/scripts/util/cloudinaryUtils');
var jobLogger = require('dw/system').Logger.getLogger('Cloudinary', 'UPLOAD');

var url;

try {
if (file.path && cloudinaryUtils.isVideo(file.path, cloudinaryConstants)) {
folder = cloudinaryConstants.CLD_ORG_CONTENT_VIDEO_PATH;
} else {
folder = cloudinaryConstants.CLD_ORG_CONTENT_IMAGE_PATH;
}

url = cloudinaryConstants.HOST_NAME + cloudinaryConstants.ORG_CONTENT_DW_URL + file.path;
jobLogger.info('Now uploading file: {0}', file.getName());
var assetPublicID = jobStepHelpers.getAssetRelURL(file.toString());
var changedAssetIds = jobStepHelpers.changePublicIdAndCloudFolder(assetPublicID, folder);
assetPublicID = changedAssetIds.assetPublicID;
var svcArgs = jobStepHelpers.getCldUploadSvcArgs();
jobStepHelpers.uploadFile(cloudinaryConstants, url, null, folder, assetPublicID, null, svcArgs, null);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this where we get 401? Can you share the log line, filename and the timestamp so I can triage it in the Cloudinary logs?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asad-rafter Thanks for the code change. Please share the output of it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yuval-cloudinary output means the logs you want?

Copy link
Collaborator

@yuval-cloudinary yuval-cloudinary Jan 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes @asad-rafter , the new logs with the 401

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yuval-cloudinary we are still getting the 401, cloudinary is still trying to access the content and it returns the 401. I have attached the latest logs.
Job-custom.cloudinary.UploadOrgStaticContent-a90b2e248d351a08de8a632441.log

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asad-rafter Was the 401 issue resolved after removing the environment protection?


return true;
} catch (err) {
jobLogger.error('Processing failure: {0}', err);
return false;
}
}

/**
* Loops over folder/file list recursively and uploads each asset
*
* @param {Object} folder - the current folder
* @param {array} arrFilelist - list of files (optional)
* @param {string} syncMode - full/delta
* @param {string} cloudinaryConstants - organization constants
*/
var processFolder = function (folder, arrFilelist, syncMode, lastJobExecution, cloudinaryUtils, cloudinaryConstants, jobLogger) {
var counter;
var file;
var files = folder.listFiles();
var filelist = arrFilelist || [];
counter = files.length;

while (counter > 0) {
file = files[counter - 1];
if (file.isDirectory()) {
jobLogger.debug('** Now processing folder: {0}', file.getName());
filelist = processFolder(file, null, syncMode, lastJobExecution, cloudinaryUtils, cloudinaryConstants, jobLogger);
} else {
filelist.push(file);
if (cloudinaryUtils.validFile(file.getName(), cloudinaryConstants)) {
jobLogger.debug('** Now processing file: {0}', file.getName());
// If this is a delta job, skip files already processed
if (syncMode === 'DELTA') {
if (file.lastModified() > lastJobExecution) {
doFile(folder.getFullPath(), file);
}
} else {
doFile(folder.getFullPath(), file);
}
}
}
counter--;
}
};

/**
* Job's starting point uploads all image, video and raws
* present at organization level into Cloudinary DAM
*
* @param {Object} args - arguments (executionMode, debugCount)
*/
function start(args) {
var System = require('dw/system/System');
var Transaction = require('dw/system/Transaction');

var cloudinaryConstants = require('~/cartridge/scripts/util/cloudinaryConstants');
var jobStepHelpers = require('~/cartridge/scripts/helpers/jobStepHelpers');
var cloudinaryUtils = require('~/cartridge/scripts/util/cloudinaryUtils');
var jobLogger = require('dw/system').Logger.getLogger('Cloudinary', 'UPLOAD');
var File = require('dw/io/File');

var debugCounter = args.debugCounter || 0;
var orgContentFolder;
var lastJobExecution = new Date(cloudinaryConstants.CLD_LAST_SYNC);
var resource;
var resources;

try {
if (cloudinaryConstants.CLD_ENABLED) {
if (jobStepHelpers.isStepDisabled(args)) {
return new Status(Status.OK, 'OK', 'Step disabled, skip it...');
}
orgContentFolder = new File(cloudinaryConstants.FORWARD_SLASH + File.STATIC + cloudinaryConstants.FORWARD_SLASH);
resources = orgContentFolder.listFiles();

if (resources) {
Transaction.wrap(function () {
var runTime = new Date();
System.preferences.custom.CLDLastSyncJobExecution = runTime;
});
for (var idx = 0; idx < resources.length; idx++) {
resource = resources[idx];
if (resource.isFile() && cloudinaryUtils.validFile(resource.getName(), cloudinaryConstants)) {
// if the job is running in "delta" mode skip already processed files
if (args.CLDSyncMode === 'DELTA') {
if (resource.lastModified() > lastJobExecution) {
doFile(resource.getName(), resource);
}
} else {
doFile(resource.getName(), resource);
}
} else {
processFolder(resource, null, args.CLDSyncMode, lastJobExecution, cloudinaryUtils, cloudinaryConstants, jobLogger);
}

if (debugCounter !== 0) {
debugCounter--;
if (debugCounter === 0) {
break;
}
}
}
};
} else {
jobLogger.error('Cloudinary is disabled currently at organization level');
}
} catch (ex) {
jobLogger.error('Error occurred while uploading organization static content on cloudinary, error : {0}, {1}, {2}', ex, ex.fileName, ex.lineNumber);
}
};

module.exports = {
Start: start
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
var Calendar = require('dw/util/Calendar');
var logger = require('dw/system/Logger').getLogger('Cloudinary', 'UPLOAD');

var cloudinaryUtils = require('*/cartridge/scripts/util/cloudinaryUtils');
var cloudinaryUtils = require('~/cartridge/scripts/util/cloudinaryUtils');

/**
* This method creates the request object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
/* API Includes */
var logger = require('dw/system/Logger').getLogger('Cloudinary', 'UPLOAD');

var cloudinaryUtils = require('*/cartridge/scripts/util/cloudinaryUtils');
var cloudinaryConstants = require('*/cartridge/scripts/util/cloudinaryConstants');
var cloudinaryUtils = require('~/cartridge/scripts/util/cloudinaryUtils');
var cloudinaryConstants = require('~/cartridge/scripts/util/cloudinaryConstants');

/**
* This method creates the request object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* API Includes */
var LocalServiceRegistry = require('dw/svc/LocalServiceRegistry');
/* Script Includes */
var cloudinaryConstants = require('*/cartridge/scripts/util/cloudinaryConstants');
var cloudinaryConstants = require('~/cartridge/scripts/util/cloudinaryConstants');
/**
* Create and configure service.
*
Expand Down
Loading