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

NIMS 1.1 #106

Merged
merged 5 commits into from
Jan 5, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add upload route
  • Loading branch information
Gunnar Schaefer authored and kevlarkevin committed Jan 5, 2015
commit a1421c7fafbc94872d09e202fe564e406543a56c
6 changes: 6 additions & 0 deletions apache.conf
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@ Alias /nims/javascript /var/local/nims/nimsgears/public/javascript
<Location /nims/logout_handler>
WebAuthDoLogout on
</Location>

<Location /testing/upload>
Order deny,allow
Deny from all
# add allowed hosts
</Location>
21 changes: 20 additions & 1 deletion nimsgears/controllers/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

import os
import json
import uuid
import hashlib
import datetime
import tempfile
import subprocess

from tg import config, expose, flash, lurl, request, redirect, response
from tg import abort, config, expose, flash, lurl, request, redirect, response
from tg.i18n import ugettext as _, lazy_ugettext as l_
import webob.exc

Expand Down Expand Up @@ -257,3 +259,20 @@ def download(self, **kwargs):
tar_proc = subprocess.Popen('tar -chf - -C %s nims; rm -r %s' % (temp_dir, temp_dir), shell=True, stdout=subprocess.PIPE)
response.content_disposition = 'attachment; filename=%s_%s' % ('nims', datetime.datetime.now().strftime('%Y%m%d_%H%M%S'))
return tar_proc.stdout

@expose()
def upload(self, filename='upload', **kwargs):
if 'Content-MD5' not in request.headers:
abort(400, 'Request must contain a valid "Content-MD5" header.')
stage_path = config.get('upload_path')
with nimsutil.TempDir(prefix='.tmp', dir=stage_path) as tempdir_path:
hash_ = hashlib.sha1()
upload_filepath = os.path.join(tempdir_path, filename)
with open(upload_filepath, 'wb') as upload_file:
for chunk in iter(lambda: request.body_file.read(2**20), ''):
hash_.update(chunk)
upload_file.write(chunk)
if hash_.hexdigest() != request.headers['Content-MD5']:
abort(400, 'Content-MD5 mismatch (or unset).')
print 'upload from %s: %s [%s]' % (request.user_agent, os.path.basename(upload_filepath), nimsutil.hrsize(request.content_length))
os.rename(upload_filepath, os.path.join(stage_path, str(uuid.uuid1()) + '_' + filename)) # add UUID to prevent clobbering files
5 changes: 3 additions & 2 deletions nimsutil/nimsutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
class TempDir(object):

"""Context managed temporary directory creation and automatic removal."""
def __init__(self, dir=None):
def __init__(self, dir=None, prefix='tmp'):
self.dir = dir
self.prefix = prefix
super(TempDir, self).__init__()

def __enter__(self):
"""Create temporary directory on context entry, returning the path."""
self.temp_dir = tempfile.mkdtemp(dir=self.dir)
self.temp_dir = tempfile.mkdtemp(dir=self.dir, prefix=self.prefix)
return self.temp_dir

def __exit__(self, exc_type, exc_value, traceback):
Expand Down