Exemple #1
0
    def inner(request, url):
        print("In inner validator")
        # Validate that the URL exists
        if not url_tools.validate_url_request(url):
            print("Invalid URL")
            return response_ko("Invalid URL")

        # Validate that the URL isn't expired
        if not url_tools.validate_url_not_expired(url):
            url_tools.expire_url(url)
            print("URL has expired")
            return response_ko("URL has expired")

        return view(request, url)
Exemple #2
0
def download(request, url):
    '''
        This method retrieves the zipped download file
    '''
    print("In download")

    urlobj = ActiveURL.objects.get(url=url)

    # Validate the request token
    token = request.GET.get('token', False)
    if token is False:
        return response_ko("Missing token")

    if not urlobj.validate_token(token):
        return response_ko("Invalid token")

    # Valiate download-ability
    if not urlobj.is_downloadable():
        #to limit number of download attempts, for security
        url_tools.expire_url(url)
        return response_ko('Limit reached')
    else:
        urlobj.inc_down_count()

    if not url_tools.url_in_media(url):
        url_tools.expire_url(url)
        return response_ko('url not found')

    if not urlobj.is_processed():
        return response_ko('url not processed')

    # Download if processed
    for files in os.listdir(
            os.path.join(settings.MEDIA_ROOT, 'scramble', 'temp', url)):
        if files.lower().endswith(('.zip')):
            prezipped = files

    prezipped_address = os.path.join(settings.MEDIA_ROOT, 'scramble', 'temp',
                                     url, prezipped)
    response = HttpResponse(open(prezipped_address, 'rb').read(),
                            content_type='application/zip')
    response['Content-Disposition'] = 'attachment; filename=' + prezipped
    return response
Exemple #3
0
def load(request, url):
    '''
        This method processes the files.
    '''
    print("In LOAD")

    url_obj = ActiveURL.objects.get(url=url)

    if url_obj.is_processed():
        return response_ko("Already loaded")

    #process files
    print("At Manager")
    url_obj.set_start()
    with ScramblerManager(url) as manager:
        manager.run()
    url_obj.set_end()

    url_obj.processed = True
    url_obj.save()

    return response_ok({"url": url})
Exemple #4
0
 def inner(request, *args, **kwargs):
     if not request.user.is_superuser:
         return response_ko("Invalid permissions")
     return function(request, *args, **kwargs)
Exemple #5
0
def post(request):
    '''
        This method accepts and stores the data for scrambling
    '''
    common_tools.show_request(request)

    form = ScrambleForm(request.POST, request.FILES)
    if not form.is_valid():
        return response_ko("Invalid form data")

    form = form.cleaned_data
    formdat = {
        'mode': form['mode'],
        'k1': form['key_one'],
        'k2': form['key_two'],
        'k3': form['key_three']
    }

    if formdat['mode'] not in ['Scramble', 'Unscramble']:
        return response_ko('Invalid mode')

    for key in ['k1', 'k2', 'k3']:
        if len(formdat[key]) < 3:
            return response_ko('Key too short')

    if not len(request.FILES.getlist('images')) > 0:
        return response_ko("No images submitted")

    # Create an ActiveURL
    this_url = ActiveURL.generate_url()
    urlobj = ActiveURL.objects.create(url=this_url)

    if formdat['mode'] == 'Unscramble':
        urlobj.mode = 'Unscramble'
    urlobj.set_token(form['retrieve_token'])
    urlobj.save()

    # Create ZipCode object
    if form.get('zipcode', False):
        zipobj = ZipLock.objects.create(active=urlobj)
        zipobj.set_zipcode(form['zipcode'])
        zipobj.save()

    # Create KeyChain object
    keyobj = KeyChain.objects.create(active=urlobj)
    keyobj.set_keys([formdat['k1'], formdat['k2'], formdat['k3']])
    keyobj.save()

    # Create the dir for storing the files
    media_path = media_tools.make_dir(this_url)

    # Store the files and create a URL item for each
    for f in request.FILES.getlist('images'):
        if f.name.lower().endswith(('.jpg', '.bmp', '.png', '.jpeg')):

            file_name, file_type = os.path.splitext(f.name.lower())
            file_path = os.path.join(media_path, f.name)

            image = Image.open(f)
            image.save(file_path, subsampling=0, quality=100)

            file_size = os.path.getsize(file_path)

            # Create the UrlItem
            url_item = UrlItem.objects.create(
                id=UrlItem.create_url_item_uuid(),
                active=urlobj,
                file_name=file_name,
                file_type=file_type[1:],
                file_size=file_size,
                file_path=file_path)
            urlobj.increment_count()

    # return success to initate the load
    return response_ok({"url": this_url})