Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

Commit

Permalink
Add studio file sha1 check to getChangedFiles method
Browse files Browse the repository at this point in the history
  • Loading branch information
Petteri Hirvonen committed Sep 13, 2017
1 parent d38a2da commit d95bf43
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 53 deletions.
59 changes: 7 additions & 52 deletions StudioHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1159,8 +1159,7 @@ class StudioHelper {
}

getChangedFiles(studioFiles, localFiles, path, studioFolderId) {
let self = this,
fileDetailsNeeded = [];
let self = this;

return new Promise(function(resolve) {
let fileUploadArray = [];
Expand All @@ -1170,27 +1169,16 @@ class StudioHelper {
localFileIndex = localFiles.indexOf(studioFile.name),
studioFileSha1 = studioFile.details && studioFile.details.sha1 || null;

console.log(studioFile);

// File found in local and studio folder
if (localFileIndex !== -1) {
let fileName = localFiles[localFileIndex],
fileStats = fs.statSync(path + '/' + fileName),
changedTime = Math.round(new Date(fileStats.mtime).getTime() / 1000);

if (studioFileSha1 !== fileStats.sha1) {

}

if (changedTime > +studioFile.createdAt) {
// If local file is newer and has different sha1, add it to upload array
if (changedTime > +studioFile.createdAt && studioFileSha1 !== fileStats.sha1) {
let fileInfo = self.getLocalFileInfo(path + '/' + fileName);

fileDetailsNeeded.push({
'id': studioFile.id,
'sha1': fileInfo.sha1,
'name': fileName
});

fileUploadArray.push({
'action': 'replace',
'folderId': studioFolderId,
Expand All @@ -1203,15 +1191,10 @@ class StudioHelper {
'data': fileInfo.data,
'createNewVersion': 1
});

// Remove it from localFiles array. We only want new files to remain there
localFiles.splice(localFileIndex, 1);
} else {
// Older local file, remove from localFiles
localFiles.splice(localFileIndex, 1);
}
} else {
//console.log('file not found');

// Remove it from localFiles array. We only want new files to remain there
localFiles.splice(localFileIndex, 1);
}
}

Expand All @@ -1233,35 +1216,7 @@ class StudioHelper {
});
}

Promise.resolve(fileDetailsNeeded).map(function(file) {
return self.getFileDetails(file.id).then(function(fileDetails) {
if (fileDetails && fileDetails.details && fileDetails.details.sha1) {
// If file has not been changed add it to removable files
if (fileDetails.details.sha1 === file.sha1) {
return file.id;
} else {
return null;
}
}
});
}).then(function(res) {
// Remove unchanged files from upload array
for (let i = res.length - 1; i >= 0; i--) {
if (res[i]) {
for (let j = fileUploadArray.length - 1; j >= 0; j--) {
if (fileUploadArray[j].id === res[i]) {
fileUploadArray.splice(j, 1);
}
}
}
}

resolve(fileUploadArray);
});

if (!fileDetailsNeeded.length) {
resolve(fileUploadArray);
}
return resolve(fileUploadArray);
});
}

Expand Down
2 changes: 1 addition & 1 deletion test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ describe('StudioHelper', function() {
});
});

it.only('should not upload unchanged files again', function () {
it('should not upload unchanged files again', function () {
return studio.push({
'folders': [{
'folderId': addedPushFolder,
Expand Down

0 comments on commit d95bf43

Please sign in to comment.