Beispiel #1
0
def recentFiles(request):
    logging.info('Handling listFiles request: %s', request.REQUEST.items())
    
    pageIndex = request.REQUEST.get('pageIndex')
    resultsPerPage = request.REQUEST.get('resultsPerPage')
    
    files = fetchRecentFiles(pageIndex, resultsPerPage)
    
    if files is not None:
        pickled = filesPickler.pickle(files, resultsPerPage)
        #logging.info('Sending response: %s', pickled)
        return HttpResponse(pickled, mimetype='application/json; charset=utf-8')

    else:
        return HttpResponse()
Beispiel #2
0
def listFiles(request):
    logging.info('Handling listFiles request: %s', request.REQUEST.items())
    
    instanceId = request.REQUEST.get('instanceId')
    pageIndex = request.REQUEST.get('pageIndex')
    resultsPerPage = request.REQUEST.get('resultsPerPage')
    
    if not pageIndex:
        logging.warning('No pageIndex in: %s ', request.REQUEST.items())
        return HttpResponseBadRequest('pageIndex required')
    
    if not resultsPerPage:
        logging.warning('No resultsPerPage!!')
        return HttpResponseBadRequest('resultsPerPage required')
    
    
    limit = int(resultsPerPage)
    offset = int(pageIndex) * limit
    
    filesQuery = models.File.all()
    filesQuery.filter('instanceId = ', int(instanceId))
    filesQuery.filter('downloaded = ', False)

    # The number of files shouldn't be too huge, but we'd ideally use a 
    # distributed counter here.
    total = filesQuery.count()
    
    filesQuery.order('title')
    files = filesQuery.fetch(limit, offset)
    
    if files is not None:
        pickled = filesPickler.pickle(files, total)
        #logging.info('Sending response: %s', pickled)
        return HttpResponse(pickled, mimetype='application/json; charset=utf-8')

    else:
        return HttpResponse()
Beispiel #3
0
def publishFileBase(request, useInstanceId):
    requestDict = request.REQUEST
    logging.info('Handling publishFile request: %s', requestDict.items())
    title = requestDict.get('title')
    #logging.info('Getting SHA-1')
    sha1 = requestDict.get('sha1')
    #etag = requestDict.get('etag')
    uri = requestDict.get('uri')
    
    size = requestDict.get('size')
    
    #logging.info('Got params')
    if title[0] == '.':
        return HttpResponseBadRequest('Cannot publish dot files')
    
    #logging.info('About to guess MIME')
    mimeType, encoding = mimetypes.guess_type(title)
    #logging.info('full mime: %s', mimeType)
    if mimeType is None:
        mimeType = 'application/octet-stream'
    mediaType = typeTranslator.getType(title);
    #logging.info('mediaType: %s', mediaType)
    
    if useInstanceId:
        #logging.info('getting instance ID')
        instanceId = int(requestDict.get('instanceId'))
        file = models.File.gql('where sha1 = :1 and instanceId = :2', sha1, instanceId).get()
    else:
        file = models.File.gql('where uri = :1 and size = :2', uri, size).get()
        
    logging.info('Existing file is: %s', file)
    
    requestTags = requestDict.get('tags')
    tagsArray = toTags(requestTags)
    
    alreadyPublished = False
    if file is None:
        form = PublishFileForm(data=requestDict)
    else:
        logging.info('File is not None!!')
        form = PublishFileForm(data=requestDict, instance=file)
        alreadyPublished = True
    if not form.is_valid():
        logging.info('Form not valid!!')
        return HttpResponse('ERROR: Publish file errors:\n%s' % repr(form.errors),
                        content_type='text/plain', status=400)
        
    file = form.save(commit=False)
    file.downloaded = util.dictStringToBool(requestDict, 'downloaded')
    file.mimeType = mimeType
    file.mediaType = mediaType
    file.tags = tagsArray
    
    metaFile = models.MetaFile.gql('where uri = :1', form.cleaned_data['uri']).get()
    if metaFile is None:
        #logging.info('Creating new meta file')
        metaForm = PublishMetaFileForm(data=requestDict)
        if not metaForm.is_valid():
            logging.info('ERROR: Publish meta file errors:\n%s' % repr(metaForm.errors))
            return HttpResponse('ERROR: Publish meta file errors:\n%s' % repr(metaForm.errors),
                        content_type='text/plain', status=400)
        metaFile = metaForm.save(commit=False)
        metaTags = title.split()
        metaTags.extend(tagsArray)
        metaFile.tags = metaTags
        metaFile.titles = [metaFile.title]
        #metaFile.onlineInstances = []
        metaFile.mimeType = mimeType
        metaFile.mediaType = mediaType
        
    else:
        #logging.info('Updating meta file')
        if title not in metaFile.titles:
            metaFile.titles.append(title)
        
        metaTags = []
        for tag in tagsArray:
            if tag not in metaFile.tags:
                logging.info('Appending tag: %s', tag)
                metaTags.append(tag)
        metaFile.tags.extend(metaTags) 

    #logging.info('Saving metaFile: %s', metaFile)
    if useInstanceId:
        instance = models.OnlineInstance.gql('where instanceId = :1', instanceId).get()
        if instance is None:
            logging.error('We know nothing about the instance')
            file.instanceOnline = False
        else:
            file.instanceOnline = True
            logging.debug('Appending LittleShoot instance')
            #if instance.key() not in metaFile.onlineInstances:
            #logging.info('Adding instance...')
            #metaFile.onlineInstances.append(instance.key())
            
            # This could be a publish request that's updating an existing file,
            # in which case it shouldn't increase the online count.
            if not alreadyPublished:
                metaFile.numOnlineInstances += 1
            
    file.put()
    metaFile.put()
    
    logging.info("Encoding file title: %s", file.title)
    encodedName = urllib.quote(file.title)
    downloadParams = {'uri': metaFile.uri,
        'name': file.title,
        'size': metaFile.size,
        
        # Careful here. In the future the URI passed to this method might 
        # not be a URN.
        'urn': metaFile.uri
        }
    encodedDownloadParams = urllib.urlencode(downloadParams)
    fullUri = 'http://www.littleshoot.org/api/client/download/'+encodedName+'?' + encodedDownloadParams
    logging.info("Encoded URL: %s", fullUri)
    
    # The URI here needs to be the full
    params = {'uri': fullUri,
        'title': file.title,
        }
    #query = urllib.urlencode(params)
    #linkUrl = "http://www.littleshoot.org/link?"+ query
    
    allProps = toNormalizedDict(metaFile)
    #allProps['link'] = linkUrl
    allProps['link'] = fullUri
    
    logging.info("Props: %s", allProps)
    
    json = simplejson.dumps(allProps)
    
    channels = simplejson.loads(memcache.get('channels') or '{}')
    
    files = fetchRecentFiles("0", "10")
    encoded_message = filesPickler.pickle(files, 10)
    
    for channel_id in channels.iterkeys():
        # encoded_message = simplejson.dumps(message)
        logging.info("Sending message on channel: %s", channel_id)
        channel.send_message(channel_id, encoded_message)
        
    return jsonControllerUtils.writeResponse(request, json)