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

Fix for nupkg assets id duplication #241

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions api/controllers/AssetController.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,13 @@ module.exports = {
sails.log.debug('Creating asset with name', data.name || uploadedFile.filename);

var hashPromise;
var packageType;

if (fileExt === '.nupkg') {
// Calculate the hash of the file, as it is necessary for windows
// files
hashPromise = AssetService.getHash(uploadedFile.fd);
packageType = AssetService.getPackageType(uploadedFile.filename);
} else if (fileExt === '.exe' || fileExt === '.zip') {
hashPromise = AssetService.getHash(uploadedFile.fd, 'sha256');
} else {
Expand All @@ -229,6 +231,7 @@ module.exports = {
name: uploadedFile.filename,
hash: fileHash,
filetype: fileExt,
package_type: packageType,
fd: uploadedFile.fd,
size: uploadedFile.size
}, data))
Expand Down
10 changes: 8 additions & 2 deletions api/models/Asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ module.exports = {
required: true
},

package_type: {
type: 'string'
},

hash: {
type: 'string'
},
Expand Down Expand Up @@ -60,9 +64,11 @@ module.exports = {
autoPK: false,

beforeCreate: (asset, proceed) => {
const { version, platform, filetype } = asset;
const { version, platform, filetype, package_type } = asset;

asset.id = `${version}_${platform}_${filetype.replace(/\./g, '')}`;
asset.id = [version, platform, filetype.replace(/\./g, ''), package_type]
.filter(arg => arg)
.join('_');

return proceed();
}
Expand Down
11 changes: 11 additions & 0 deletions api/services/AssetService.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ AssetService.getHash = function(fd, type = 'sha1') {
});
};

/**
* Returns nupkg type (full, delta) from filename
*/
AssetService.getPackageType = function(filename) {
var matches = filename.match(/.*-(\w+)\.nupkg/);

return matches && matches.length === 2
? matches[1]
: undefined;
}


/**
* Deletes an asset from the database.
Expand Down
30 changes: 30 additions & 0 deletions migrations/20200520000000-asset-package-type-migration copy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const { driver } = require('db-migrate').getInstance().config.getCurrent().settings;

const sql = {

// PostgreSQL
pg: {
up: (

// Add `package_type` column (if doesn't exist) to `asset` table
'ALTER TABLE asset ADD COLUMN IF NOT EXISTS package_type TEXT;'

),
down: (


// Drop `package_type` column (if exists) from `asset` table
'ALTER TABLE asset DROP COLUMN IF EXISTS package_type;'

)
}

};

const { up } = driver && sql[driver];
const { down } = driver && sql[driver];

exports.up = db => up ? db.runSql(up) : null;
exports.down = db => down ? db.runSql(down) : null;