Ejemplo n.º 1
0
 def _save(self, name, content):
     name = normalize_name(name)
     try:
         dbfile = DBFile.objects.get(file_name=name)
     except DBFile.DoesNotExist:
         dbfile = DBFile(file_name=name)
     raw_data = content.read()
     dbfile.size = len(raw_data)
     dbfile.data = raw_data # should probably do this in chunks
     dbfile.save()
     del raw_data
     return name
Ejemplo n.º 2
0
 def _save(self, name, content):
     name = normalize_name(name)
     try:
         dbfile = DBFile.objects.get(file_name=name)
     except DBFile.DoesNotExist:
         dbfile = DBFile(file_name=name)
     raw_data = content.read()
     dbfile.size = len(raw_data)
     dbfile.data = raw_data
     dbfile.save()
     cache.set(name, dbfile, self.CACHE_TIMEOUT)
     del raw_data
     return name
Ejemplo n.º 3
0
def load():
    # uploading
    if (request.method == 'POST'):
        uploaded_files = request.files.getlist('file')
        if (not uploaded_files):
            resultFailure("no files recieved.", 400)
        # getting all the parameters
        params = request.args.to_dict()
        masterID = None
        # removing the masterid paramerter if it is there
        if ('masterid' in params):
            masterID = params.pop('masterid')
        # if not, then use the user's guid
        else:
            masterID = g.user.guid

        code = params.pop('code') if 'code' in params else ""
        codeList = code.split(',')
        listLength = len(codeList)
        if (listLength != len(uploaded_files)):
            return resultFailure(msg="Code and file numbers mistmatch.",
                                 code=400)
        if (listLength > 1):
            code = codeList

        fileList = []
        for i in range(0, len(uploaded_files)):
            file = uploaded_files[i]
            fileNameList = file.filename.split('.')
            filename = fileNameList[0]
            fileext = fileNameList[1]
            fileItem = DBFile.query.filter_by(
                masterid=masterID,
                code=(code[i] if type(code) is list else code)).first()
            if (fileItem is None):
                fileguid = uuid.uuid4()
                fileItem = DBFile(
                    masterid=masterID,
                    guid=fileguid,
                    filename=filename,
                    hotelrefno=0,
                    filetype=fileext,
                    code=(code[i] if type(code) is list else code))
            os.makedirs(os.path.join(sys.path[0] + "/storage/dbfile",
                                     str(masterID)),
                        exist_ok=True)
            file.save(
                os.path.join(sys.path[0] + '/storage/dbfile', str(masterID),
                             str(fileItem.guid) + '.' + fileext))
            db.session.add(fileItem)
            fileList.append(fileItem.to_dict())
        db.session.commit()
        # returning the normal response + getting the non popped parameters into spurious
        return extensions.dataResultSuccess(fileList,
                                            count=len(fileList),
                                            spuriousParameters=list(
                                                params.keys()),
                                            code=201)

    else:  # Downloading
        arguments = request.args.to_dict()
        dbFileItem = None
        if ('id' in arguments):
            dbFileItem = DBFile.query.filter_by(id=arguments['id']).first()
        elif ('guid' in arguments):
            dbFileItem = DBFile.query.filter_by(guid=arguments['guid']).first()
        elif (('masterid' in arguments) & ('code' in arguments)):
            dbFileItem = DBFile.query.filter_by(
                masterid=arguments['masterid'],
                code=arguments['code']).first()
        else:
            resultFailure(
                "no identifier provided, you need to supply ID, GUID, or MasterID & Code.",
                400)
        if (dbFileItem is None):
            resultFailure("DBFile not found.", 404)
        return send_from_directory(
            os.path.join(sys.path[0], 'storage/dbfile',
                         str(dbFileItem.masterid)),
            (str(dbFileItem.guid) + '.' + dbFileItem.filetype))