예제 #1
0
def storeLinks(linkList, itemInstance, chunkSize, keyData):
    if len(linkList) > 0:
        key = EncryptionKey.getOrCreate(chunkSize=chunkSize, keydata=keyData)
        key.save()
        downloadFileGroup = DownloadFileGroup(itemInstance=itemInstance, key=key)
        downloadFileGroup.save()
        for link in linkList:
            df = DownloadFile(downloadFileGroup=downloadFileGroup,
                              downloadLink=link)
            df.save()
예제 #2
0
def handleItemForm(request, form, item):
    error = None

    language = Language.getOrCreate(form.cleaned_data['language'])
    source = Source.getOrCreate(form.cleaned_data['source'])
    filesize = None
    if form.cleaned_data['size']:
        filesize = form.cleaned_data['size']

    itemInstance = ItemInstance.getOrCreate(size=filesize,
                                            language=language,
                                            source=source,
                                            creator=request.user,
                                            item=item)
    itemInstance.save()

    if form.cleaned_data['file']:
        itemInstance.path = generateDestinationPath(form)
        if form.cleaned_data['tar']:
            tf = tarfile.open(fileobj=form.cleaned_data['file'])
            tf.extractall(path=path)
        else:
            destination = open(path+os.path.basename(form.cleaned_data['file'].name), 'wb+')
            for chunk in form.cleaned_data['file'].chunks():
                destination.write(chunk)
            destination.close()
        itemInstance.size = form.cleaned_data['file'].size
        itemInstance.mtime = int(os.stat(path).st_mtime)
        itemInstance.present = True
        itemInstance.save()
        form = None
        error = 'Thank you for contributing.'
    elif (form.cleaned_data['dlLinks'] or form.cleaned_data['dlLinksFile']) and form.cleaned_data['keyfile']:
        dlUrls = []
        if form.cleaned_data['dlLinks']:
            dlUrls = [ url.strip() for url in form.cleaned_data['dlLinks'].split(',')]
        else:
            dlUrls = [ url.strip() for url in form.cleaned_data['dlLinksFile'] ]
        if len(dlUrls) > 0:
            key = EncryptionKey.fromFileHandle(form.cleaned_data['keyfile'])
            downloadFileGroup = DownloadFileGroup(itemInstance=itemInstance, key=key)
            downloadFileGroup.save()
            for url in dlUrls:
                df = DownloadFile(downloadFileGroup=downloadFileGroup, downloadLink=url)
                df.save()
        form = None
        error = 'Thank you for contributing.'
    else:
        form = None
        error = "Thank you for enlisting your item. You will be contacted when someone requests this item."
    return form, error
예제 #3
0
def addLinks(itemInstance, form):
    msg = "Unable to add new links: Invalid form data."
    success = False
    if form.is_valid():
        if form.cleaned_data['dlLinks'] or form.cleaned_data['dlLinksFile']:
            dlUrls = []
            if form.cleaned_data['dlLinks']:
                dlUrls = [ url.strip() for url in form.cleaned_data['dlLinks'].split(',')]
            else:
                dlUrls = [ url.strip() for url in form.cleaned_data['dlLinksFile'] ]
            if len(dlUrls) > 0:
                key = EncryptionKey.fromFileHandle(form.cleaned_data['keyfile'])
                downloadFileGroup = DownloadFileGroup(itemInstance=itemInstance, key=key)
                downloadFileGroup.save()
                for url in dlUrls:
                    df = DownloadFile(downloadFileGroup=downloadFileGroup, downloadLink=url)
                    df.save()
                msg = "Thanks for uploading new links."
                success = True
            else:
                msg = "Unable to add new links: No download links found."
        else:
            msg = "Unable to add new links: Neither download links nor download links file given."
    return msg, success