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

File already in Hatrac, update with new filename #1009

Merged
merged 5 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
69 changes: 68 additions & 1 deletion js/hatrac.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ var ERMrest = (function(module) {
})(ERMrest || {});

var ERMrest = (function(module) {
var FILENAME_REGEXP = /[^a-zA-Z0-9_.-]/ig;

var allowedHttpErrors = [500, 503, 408, 401];

Expand Down Expand Up @@ -310,6 +311,7 @@ var ERMrest = (function(module) {

this.http = this.reference._server.http;

this.updateDispositionOnly = false;
this.isPaused = false;
this.otherInfo = otherInfo;

Expand Down Expand Up @@ -430,13 +432,26 @@ var ERMrest = (function(module) {
var headers = module.getResponseHeader(response);
var md5 = headers["content-md5"];
var length = headers["content-length"];
var contentDisposition = headers["content-disposition"];

// If the file is not same, then simply resolve the promise without setting completed and jobDone
if ((md5 != self.hash.md5_base64) || (length != self.file.size)) {
deferred.resolve(self.url);
return;
}

// hatrac only supports `filename*=UTF-8''` in the content disposition so check for this string to get the filename
var contentDispositionPrefix = "filename*=UTF-8''";
var filenameIndex = contentDisposition.indexOf(contentDispositionPrefix) + contentDispositionPrefix.length;

// check if filename in content disposition is different from filename being uploaded
// if it is, instead set a different flag and don't mark it as done
jrchudy marked this conversation as resolved.
Show resolved Hide resolved
if (contentDisposition.substring(filenameIndex, contentDisposition.length) != self.file.name.replace(FILENAME_REGEXP, '_')) {
self.updateDispositionOnly = true;
deferred.resolve(self.url);
return;
}

self.isPaused = false;
self.completed = true;
self.jobDone = true;
Expand Down Expand Up @@ -496,7 +511,7 @@ var ERMrest = (function(module) {
"content-length": self.file.size,
"content-type": self.file.type,
"content-md5": self.hash.md5_base64,
"content-disposition": "filename*=UTF-8''" + self.file.name.replace(/[^a-zA-Z0-9_.-]/ig, '_')
"content-disposition": "filename*=UTF-8''" + self.file.name.replace(FILENAME_REGEXP, '_')
};

if (!contextHeaderParams || !_isObject(contextHeaderParams)) {
Expand Down Expand Up @@ -528,6 +543,58 @@ var ERMrest = (function(module) {
return deferred.promise;
};

/**
* @desc Call this function to create an update metadata request for updating the content-disposition
*
* @returns {Promise} A promise resolved with no value or the url if it already exists
* or rejected with error
*/
upload.prototype.createUpdateMetadataJob = function (contextHeaderParams) {
var self = this;
this.erred = false;

var deferred = module._q.defer();

if (this.completed && this.jobDone) {
deferred.resolve(this.chunkUrl);
return deferred.promise;
}

// 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, '_');

if (!contextHeaderParams || !_isObject(contextHeaderParams)) {
contextHeaderParams = {
action: "upload/metadata/update",
referrer: self.reference.defaultLogInfo
};
contextHeaderParams.referrer.column = self.column.name;
}

var config = {
headers: _generateContextHeader(contextHeaderParams)
};
config.headers['content-type'] = 'text/plain';

self.http.put(url, data, config).then(function (response) {
if (response) {
// mark as completed and job done since this is a metadata update request that doesn't transfer file data
self.isPaused = false;
self.completed = true;
self.jobDone = true;

deferred.resolve();
}
}, function(response) {
var error = module.responseToError(response);
deferred.reject(error);
});

return deferred.promise;
}


/**
* @desc Call this function to start chunked upload to server. It reads the file and divides in into chunks
Expand Down
Loading