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

uploadGeneratetoken: Add support to optionally create new revision #146

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

jcfr
Copy link
Contributor

@jcfr jcfr commented Oct 27, 2015

This commit updates the "uploadGeneratetoken" API introducing the new
parameter "create_additional_revision" allowing to optionally change
the default behavior and ensure a new revision is created when:

  • a bitstream associated with the file already exists on the server
  • and the checksum parameter is specified
  • and the item updated has at least one revision

Since the http uploader doesn't specify the "checksum" parameter, the
problem is only reproducible using the web API.

For example, considering the following configuration:

 folder
   |--- item1 --- revision1
   |                 |--- bitstream1 (filename1)
   |
   |--- item2 --- revision1
   |                 |--- bitstream2 (filename2)

By default, trying to associate bitstream1 with item2 will result in
this configuration:

 folder
   |--- item1 --- revision1
   |                 |--- bitstream1 (filename1)
   |
   |--- item2 --- revision1
   |                 |--- bitstream2 (filename2)
   |                 |--- bitstream1 (filename1)

By setting the option "create_additional_revision" to True, the
following configuration will be obtained:

 folder
   |--- item1 --- revision1
   |                 |--- bitstream1 (filename1)
   |
   |--- item2 -|--- revision1
   |           |     |--- bitstream2 (filename2)
   |           |
   |           |--- revision2
   |           |     |--- bitstream1 (filename1)

This commit updates the "uploadGeneratetoken" API introducing the new
parameter "create_additional_revision" allowing to optionally change
the default behavior and ensure a new revision is created when:
  - a bitstream associated with the file already exists on the server
  - and the checksum parameter is specified
  - and the item updated has at least one revision

Since the http uploader doesn't specify the "checksum" parameter, the
problem is only reproducible using the web API.

For example, considering the following configuration:

 folder
   |--- item1 --- revision1
   |                 |--- bitstream1 (filename1)
   |
   |--- item2 --- revision1
   |                 |--- bitstream2 (filename2)

By default, trying to associate bitstream1 with item2 will result in
this configuration:

 folder
   |--- item1 --- revision1
   |                 |--- bitstream1 (filename1)
   |
   |--- item2 --- revision1
   |                 |--- bitstream2 (filename2)
   |                 |--- bitstream1 (filename1)

By setting the option "create_additional_revision" to True, the
following configuration will be obtained:

 folder
   |--- item1 --- revision1
   |                 |--- bitstream1 (filename1)
   |
   |--- item2 -|--- revision1
   |           |     |--- bitstream2 (filename2)
   |           |
   |           |--- revision2
   |           |     |--- bitstream1 (filename1)
@jcfr
Copy link
Contributor Author

jcfr commented Oct 27, 2015

Cc: @jamiesnape @cpatrick

@jcfr
Copy link
Contributor Author

jcfr commented Oct 27, 2015

The following snippet allow to reproduce the configuration:

%reload_ext autoreload
%autoreload 1
%aimport pydas
import hashlib
import os
import time

midas_url = 'http://slicer.kitware.com/midas3'
email = os.environ["MIDAS_PACKAGE_EMAIL"]
apikey = os.environ["MIDAS_PACKAGE_API_KEY"]
communicator = pydas.core.Communicator(midas_url)

folder_id = 1654 # Update this with a folder you have write access

def token(email, apikey):
    return communicator.login_with_api_key(email, apikey)

def md5(fname):
    hash = hashlib.md5()
    with open(fname) as f:
        for chunk in iter(lambda: f.read(4096), ""):
            hash.update(chunk)
    return hash.hexdigest()

def generate_file(filepath):
    with open(filepath, 'wb') as fout:
        fout.write(os.urandom(1024))
    filename = os.path.basename(filepath)
    checksum = md5(filepath)
    print("filepath: %s, md5: %s" % (filepath, checksum))
    return (filename, checksum)

prefix = str(int(time.time()))

# Create original file for item #1
filepath_item1 = './%s-bistream-1.bin' % prefix
(filename_item1, checksum_item1) = generate_file(filepath_item1)

# Create item #1
item = communicator.create_item(token(email, apikey), '%s-item1' % prefix, folder_id)
item_id = item['item_id']
print("item #1 created: %s" % (item_id))

# Upload initial revision into item #1
upload_token = communicator.generate_upload_token(token(email, apikey), item_id, filename_item1)
communicator.perform_upload(upload_token, filename_item1, item_id=item_id, filepath=filepath_item1)
print("item #1: initial revision created")

# Create original file for item #2
filepath_item2 = './%s-bistream-2.bin' % prefix
(filename_item2, checksum_item2) = generate_file(filepath_item2)

# Create item #2
item = communicator.create_item(token(email, apikey), '%s-item2' % prefix, folder_id)
item_id = item['item_id']
print("item #2 created: %s" % (item_id))

# Upload initial revision into item #2
upload_token = communicator.generate_upload_token(token(email, apikey), item_id, filename_item2)
communicator.perform_upload(upload_token, filename_item2, item_id=item_id, filepath=filepath_item2)
print("item #2: initial revision created")

# Upload an other revision into item #2 associating bitstream created for item #1
upload_token = communicator.generate_upload_token(token(email, apikey), item_id, filename_item1, checksum=checksum_item1)
print("item #2: other revision created")
if upload_token:
    raise Exception('item #2: upload_token is expected to be empty')

Item1:

item1-before-pr-update-uploadgeneratetoken-with-create-additional-revision

Item2:

item2-before-pr-update-uploadgeneratetoken-with-create-additional-revision

jcfr added a commit to Slicer/Midas that referenced this pull request Oct 27, 2015
Backported from PR midasplatform#146

This commit updates the "uploadGeneratetoken" API introducing the new
parameter "create_additional_revision" allowing to optionally change
the default behavior and ensure a new revision is created when:
  - a bitstream associated with the file already exists on the server
  - and the checksum parameter is specified
  - and the item updated has at least one revision

Since the http uploader doesn't specify the "checksum" parameter, the
problem is only reproducible using the web API.

For example, considering the following configuration:

 folder
   |--- item1 --- revision1
   |                 |--- bitstream1 (filename1)
   |
   |--- item2 --- revision1
   |                 |--- bitstream2 (filename2)

By default, trying to associate bitstream1 with item2 will result in
this configuration:

 folder
   |--- item1 --- revision1
   |                 |--- bitstream1 (filename1)
   |
   |--- item2 --- revision1
   |                 |--- bitstream2 (filename2)
   |                 |--- bitstream1 (filename1)


By setting the option "create_additional_revision" to True, the
following configuration will be obtained:

 folder
   |--- item1 --- revision1
   |                 |--- bitstream1 (filename1)
   |
   |--- item2 -|--- revision1
   |           |     |--- bitstream2 (filename2)
   |           |
   |           |--- revision2
   |           |     |--- bitstream1 (filename1)
jcfr added a commit to jcfr/pydas that referenced this pull request Oct 27, 2015
…ional_revision"

This commit add supporte from parameter introduced in midasplatform/Midas#146
@jcfr
Copy link
Contributor Author

jcfr commented Oct 27, 2015

And now by using an updated pydas adding support for create_additional_revision to generate_upload_token (See midasplatform/pydas#29) and updating the snippet posted in #146 (comment) changing:

# Upload an other revision into item #2 associating bitstream created for item #1
upload_token = communicator.generate_upload_token(token(email, apikey), item_id, filename_item1, checksum=checksum_item1)

with

# Upload an other revision into item #2 associating bitstream created for item #1
upload_token = communicator.generate_upload_token(token(email, apikey), item_id, filename_item1, checksum=checksum_item1, create_additional_revision=True)

If you scroll right, you can see the introduction of create_additional_revision=True

We obtain the expected results:

Item 1:

item1-after-pr-update-uploadgeneratetoken-with-create-additional-revision

Item 2:

item2-after-pr-update-uploadgeneratetoken-with-create-additional-revision

This commit improves the support for "create_additional_revision"
introduced in a previous commit. It will not create an additional
revision if last revision has one bitstream and checkum matches
jcfr added a commit to Slicer/Midas that referenced this pull request Oct 27, 2015
Backported from PR midasplatform#146

This commit improves the support for "create_additional_revision"
introduced in a previous commit. It will not create an additional
revision if last revision has one bitstream and checkum matches
@jcfr
Copy link
Contributor Author

jcfr commented Oct 27, 2015

I just realized the test are broken, I will fix the style.

// do not create an additional revision if last revision has one bitstream and checkum matches
if ($create_additional_revision && $revision) {
$bitstreams = $revision->getBitstreams();
if (count($bitstreams) == 1 && $args['checksum'] == $bitstreams[0]->getChecksum()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

===

@jcfr
Copy link
Contributor Author

jcfr commented Oct 27, 2015

Thanks for your review, comments have been addressed in a follow up commit.

@jcfr jcfr force-pushed the update-uploadGeneratetoken-with-create-additional-revision branch 2 times, most recently from 1fe3c0a to 730712e Compare October 27, 2015 21:17
@jcfr jcfr force-pushed the update-uploadGeneratetoken-with-create-additional-revision branch from 730712e to 4139b77 Compare October 27, 2015 21:18
@jcfr
Copy link
Contributor Author

jcfr commented Oct 27, 2015

And now Style-CI is 😄

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants