Skip to content

Commit

Permalink
use filename_basename. make sure basename is everything esxcept our s…
Browse files Browse the repository at this point in the history
…et filename_ext
  • Loading branch information
jrchudy committed Mar 27, 2024
1 parent ae82237 commit e32f76d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/user-docs/annotation.md
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ Default heuristics:
- `size` for size in bytes
- `mimetype` for mimetype of the selected file.
- `filename` for filename. NOTE: this is the filename of the uploaded file before `stored_filename_pattern` is generated
- `filename_stem` for the filename without the extension
- `filename_basename` for the filename without the extension. Note: the following will be true `filename_basename + filename_ext = filename`
- `filename_ext` for the file extension based on the filename. This value is derived based on the optionally defined `filename_ext_filter` and `filename_ext_regexp`. If these annotations are missing, the last part of the filename after the last dot will be returned (also includes the `.` e.g. `.png`).
- If we cannot find matches, this property will return `null`. So make sure you're doing null checking while using this property (otherwise, the whole `url_pattern` might result in an empty string).
- Nothing may be inferred without additional payload patterns present.
Expand Down
15 changes: 8 additions & 7 deletions js/hatrac.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,10 @@ var ERMrest = (function(module) {

// check if filename in content disposition is different from filename being uploaded
// if it is, create an update metadata request for updating the content-disposition
if (contentDisposition.substring(filenameIndex, contentDisposition.length) != self.file.name.replace(FILENAME_REGEXP, '_')) {
if (contentDisposition.substring(filenameIndex, contentDisposition.length) != self.storedFilename.replace(FILENAME_REGEXP, '_')) {
// Prepend the url with server uri if it is relative
var url = self._getAbsoluteUrl(self.url + ";metadata/content-disposition");
var data = "filename*=UTF-8''" + self.file.name.replace(FILENAME_REGEXP, '_');
var data = "filename*=UTF-8''" + self.storedFilename.replace(FILENAME_REGEXP, '_');
contextHeaderParams.action = "upload/metadata/update"

var config = {
Expand Down Expand Up @@ -835,12 +835,13 @@ var ERMrest = (function(module) {
row[this.column.name].md5_base64 = this.hash.md5_base64;
row[this.column.name].sha256 = this.hash.sha256;
row[this.column.name].filename = this.file.name;
row[this.column.name].filename_ext = _getFilenameExtension(this.file.name, this.column.filenameExtFilter, this.column.filenameExtRegexp);
// filename_stem is everything from the file name except the last ext
// For example if we have a file nameed "file.tar.zip"
// => "file.tar" is the stem
var filename_ext = _getFilenameExtension(this.file.name, this.column.filenameExtFilter, this.column.filenameExtRegexp);
row[this.column.name].filename_ext = filename_ext
// filename_basename is everything from the file name except the last ext
// For example if we have a file named "file.tar.zip"
// => "file.tar" is the basename
// => ".zip" is the extension
row[this.column.name].filename_stem = this.file.name.substring(0, this.file.name.lastIndexOf('.'));
row[this.column.name].filename_basename = this.file.name.substring(0, this.file.name.indexOf(filename_ext));

// Generate url

Expand Down
2 changes: 1 addition & 1 deletion test/specs/upload/conf/upload/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@
},
"annotations": {
"tag:isrd.isi.edu,2017:asset": {
"url_pattern" : "/hatrac/js/ermrestjs/{{{_uri.filename_stem}}}/{{{_uri.md5_hex}}}",
"url_pattern" : "/hatrac/js/ermrestjs/{{{_uri.filename_basename}}}/{{{_uri.md5_hex}}}",
"filename_column" : "filename",
"byte_count_column" : "bytes",
"md5" : "checksum",
Expand Down
8 changes: 4 additions & 4 deletions test/specs/upload/tests/01.checksum.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,10 @@ exports.execute = function (options) {
column, reference, uploadObj, ermRest;

var file = {
name: "testfile500kb.png",
name: "testfile500kb.png.zip",
size: 512000,
displaySize: "500KB",
type: "image/png",
type: "application/zip",
hash: "4b178700e5f3b15ce799f2c6c1465741",
hash_64: "SxeHAOXzsVznmfLGwUZXQQ=="
};
Expand Down Expand Up @@ -247,11 +247,11 @@ exports.execute = function (options) {
uploadObj.calculateChecksum(validRow).then(function(url) {
expect(uploadObj.hash instanceof ermRest.Checksum).toBeTruthy("Upload object hash is not of type ermRest.Checksum");

expect(url).toBe("/hatrac/js/ermrestjs/testfile500kb/" + file.hash, "File generated url is not the same");
expect(url).toBe("/hatrac/js/ermrestjs/testfile500kb.png/" + file.hash, "File generated url is not the same");

// values that are attached to the row
expect(validRow.filename).not.toBe(file.name, "valid row filename is the same as original file's name");
expect(validRow.filename).toBe(time + ".png", "valid row filename was not generated properly");
expect(validRow.filename).toBe(time + ".zip", "valid row filename was not generated properly");
expect(validRow.bytes).toBe(file.size, "valid row bytes is incorrect");
expect(validRow.checksum).toBe(file.hash, "valid row checksum is incorrect");

Expand Down

0 comments on commit e32f76d

Please sign in to comment.