Ejemplo n.º 1
0
    def patch(self, email):
        if 'email' not in session:
            return make_response(jsonify({'status': 'not logged in'}), 403)

        check_if_admin()
        if session['admin_status'] == 0:
            return make_response(jsonify({'status': 'not admin'}), 403)

        if not request.json or not 'admin_status' in request.json:
            return make_response(jsonify({'status': 'no request data'}), 400)

        new_admin_status = request.json['admin_status']
        # print(new_admin_status, type(new_admin_status), email)
        dbConnection = pymysql.connect(settings.MYSQL_HOST,
                                       settings.MYSQL_USER,
                                       settings.MYSQL_PASSWD,
                                       settings.MYSQL_DB,
                                       charset='utf8mb4',
                                       cursorclass=pymysql.cursors.DictCursor)

        sql = 'setUser'
        try:
            cursor = dbConnection.cursor()
            cursor.callproc(sql, [email, email, new_admin_status])
            dbConnection.commit()  #NEEDED for updates and inserts
        except pymysql.err.InternalError:
            return make_response(jsonify({'status': 'no change to ' + email}),
                                 200)
        except:
            abort(500)  # Nondescript server error
        finally:
            cursor.close()
            dbConnection.close()
        return make_response(jsonify({'status': 'updated ' + email}), 200)
Ejemplo n.º 2
0
    def get(self, email, file):
        if (settings.APP_HOST == '127.0.0.1'):
            #     with open('session.json') as f:
            #         session = json.load(f)
            session = settings.SESSION
        else:
            from flask import session
        if 'email' not in session:
            return make_response(jsonify({'status': 'not logged in'}), 403)

        check_if_admin()
        # if(settings.APP_HOST == '127.0.0.1'):
        #     with open('session.json') as f:
        #         session = json.load(f)
        if session['email'] != email and session['admin_status'] == 0:
            return make_response(
                jsonify(
                    {'status':
                     'not logged in as ' + email + ' and not admin'}), 403)

        stream = request.args.get('stream', 'false')
        print("pre db")
        dbConnection = pymysql.connect(host=settings.MYSQL_HOST,
                                       user=settings.MYSQL_USER,
                                       passwd=settings.MYSQL_PASSWD,
                                       db=settings.MYSQL_DB,
                                       charset='utf8mb4',
                                       cursorclass=pymysql.cursors.DictCursor)

        sql = 'getFile'
        try:
            cursor = dbConnection.cursor()
            cursor.callproc(sql, [file])  # stored procedure, arguments
            row = cursor.fetchone()
        except:
            abort(500)  # Nondescript server error
        finally:
            cursor.close()
            dbConnection.close()
        print("post db", row)
        if row is None:
            return make_response(jsonify({'status': 'no folder'}), 404)
        if (row['owner_email'] == session['email']
                or session['admin_status'] == 1):
            print("pre stream")
            if stream == 'true':
                filepath = safe_join(settings.UPLOAD_FOLDER,
                                     str(row["file_id"]))
                response = send_file(filename_or_fp=filepath,
                                     mimetype="application/octet-stream",
                                     as_attachment=True,
                                     attachment_filename=row["file_name"])
                print("sending stream")
                return response
            else:
                return make_response(jsonify({'file': row}),
                                     200)  # turn set into json and return it
        else:
            return make_response(jsonify({'status': 'not owner'}), 403)
Ejemplo n.º 3
0
    def patch(self, email):
        if(settings.APP_HOST == '127.0.0.1'):
        #     with open('session.json') as f:
        #         session = json.load(f)
            session = settings.SESSION
        else:
            from flask import session

        if 'email' not in session:
            return make_response(jsonify({'status': 'not logged in'}), 403)
            
        check_if_admin()
        if session['email'] != email and session['admin_status'] == 0:
            return make_response(jsonify({'status': 'not logged in as '+email+' and not admin'}), 403)

        if not request.json or not 'email' in request.json:
            return make_response(jsonify({'status': 'no request'}), 400)

        new_email = request.json['email'].lower()

        dbConnection = pymysql.connect(
            host = settings.MYSQL_HOST,
            user = settings.MYSQL_USER,
            passwd = settings.MYSQL_PASSWD,
            db = settings.MYSQL_DB,
            charset='utf8mb4',
            cursorclass= pymysql.cursors.DictCursor)

        sql = 'setUser'
        try:
            cursor = dbConnection.cursor()
            cursor.callproc(sql, [email, new_email, session['admin_status']])
            dbConnection.commit() #NEEDED for updates and inserts
        except pymysql.err.InternalError as e:
            if email != new_email:
                # print(e)
                return make_response(jsonify({'status':new_email+' in use or '+email+' not in use'}), 400)
            return make_response(jsonify({'status':'no change to '+email}), 200)
        except:
            abort(500) # Nondescript server error
        finally:
            cursor.close()
            dbConnection.close()

        if email == session['email']:
            session['email'] = new_email
        return make_response(jsonify({'status':'changed '+email+' to '+new_email}), 204)
Ejemplo n.º 4
0
    def get(self, email, folder):
        if (settings.APP_HOST == '127.0.0.1'):
            #     with open('session.json') as f:
            #         session = json.load(f)
            session = settings.SESSION
        else:
            from flask import session
        if 'email' not in session:
            return make_response(jsonify({'status': 'not logged in'}), 403)

        check_if_admin()
        # if(settings.APP_HOST == '127.0.0.1'):
        #     with open('session.json') as f:
        #         session = json.load(f)
        if session['email'] != email and session['admin_status'] == 0:
            return make_response(
                jsonify(
                    {'status':
                     'not logged in as ' + email + ' and not admin'}), 403)

        dbConnection = pymysql.connect(host=settings.MYSQL_HOST,
                                       user=settings.MYSQL_USER,
                                       passwd=settings.MYSQL_PASSWD,
                                       db=settings.MYSQL_DB,
                                       charset='utf8mb4',
                                       cursorclass=pymysql.cursors.DictCursor)

        sql = 'getFolder'
        try:
            cursor = dbConnection.cursor()
            cursor.callproc(sql, [folder])  # stored procedure, arguments
            row = cursor.fetchone()
        except:
            abort(500)  # Nondescript server error
        finally:
            cursor.close()
            dbConnection.close()
        print(row)
        if row is None:
            return make_response(jsonify({'status': 'no folder'}), 404)
        elif (row['owner_email'] == session['email']
              or session['admin_status'] == 1):
            return make_response(jsonify({'folder': row}),
                                 200)  # turn set into json and return it
        else:
            return make_response(jsonify({'status': 'not owner'}), 403)
Ejemplo n.º 5
0
    def get(self, email):
        if (settings.APP_HOST == '127.0.0.1'):
            #     with open('session.json') as f:
            #         session = json.load(f)
            session = settings.SESSION
        else:
            from flask import session
        if 'email' not in session:
            return make_response(jsonify({'status': 'not logged in'}), 403)
        print('sess0=', session)
        check_if_admin()
        # if(settings.APP_HOST == '127.0.0.1'):
        #     with open('session.json') as f:
        #         session = json.load(f)
        print('sess=', session)
        if session['email'] != email and session['admin_status'] == 0:
            return make_response(
                jsonify(
                    {'status':
                     'not logged in as ' + email + ' and not admin'}), 403)

        print(request)
        string = request.args.get('string', "%")

        dbConnection = pymysql.connect(host=settings.MYSQL_HOST,
                                       user=settings.MYSQL_USER,
                                       passwd=settings.MYSQL_PASSWD,
                                       db=settings.MYSQL_DB,
                                       charset='utf8mb4',
                                       cursorclass=pymysql.cursors.DictCursor)

        sql = 'findFileString'
        try:
            cursor = dbConnection.cursor()
            cursor.callproc(sql, [email, string])
            rows = cursor.fetchall()  # get all the results
        except:
            abort(500)  # Nondescript server error
        finally:
            cursor.close()
            dbConnection.close()
        return make_response(jsonify({'files': rows}),
                             200)  # turn set into json and return it
Ejemplo n.º 6
0
    def get(self):
        if (settings.APP_HOST == '127.0.0.1'):
            #     with open('session.json') as f:
            #         session = json.load(f)
            session = settings.SESSION
        else:
            from flask import session

        if 'email' not in session:
            return make_response(jsonify({'status': 'not logged in'}), 403)

        check_if_admin()
        if session['admin_status'] == 0:
            return make_response(jsonify({'status': 'not admin'}), 403)

        # dbConnection = pymysql.connect(
        #     settings.MYSQL_HOST,
        #     settings.MYSQL_USER,
        #     settings.MYSQL_PASSWD,
        #     settings.MYSQL_DB,
        #     charset='utf8mb4',
        #     cursorclass= pymysql.cursors.DictCursor)

        dbConnection = pymysql.connect(host=settings.MYSQL_HOST,
                                       user=settings.MYSQL_USER,
                                       passwd=settings.MYSQL_PASSWD,
                                       db=settings.MYSQL_DB,
                                       charset='utf8mb4',
                                       cursorclass=pymysql.cursors.DictCursor)

        sql = 'getUsers'
        try:
            cursor = dbConnection.cursor()
            cursor.callproc(sql)  # stored procedure, no arguments
            rows = cursor.fetchall()  # get all the results
        except:
            abort(500)  # Nondescript server error
        finally:
            cursor.close()
            dbConnection.close()
        return make_response(jsonify({'users': rows}),
                             200)  # turn set into json and return it
Ejemplo n.º 7
0
    def delete(self, email):
        if(settings.APP_HOST == '127.0.0.1'):
        #     with open('session.json') as f:
        #         session = json.load(f)
            session = settings.SESSION
        else:
            from flask import session

        if 'email' not in session:
            return make_response(jsonify({'status': 'not logged in'}), 403)
            
        check_if_admin()
        if session['email'] != email and session['admin_status'] == 0:
            return make_response(jsonify({'status': 'not logged in as '+email+' and not admin'}), 403)

        dbConnection = pymysql.connect(
            host = settings.MYSQL_HOST,
            user = settings.MYSQL_USER,
            passwd = settings.MYSQL_PASSWD,
            db = settings.MYSQL_DB,
            charset='utf8mb4',
            cursorclass= pymysql.cursors.DictCursor)

        sql = 'deleteUser'
        try:
            cursor = dbConnection.cursor()
            cursor.callproc(sql, [email])
            dbConnection.commit() #NEEDED for updates and inserts
        except pymysql.err.InternalError as e:
            return make_response(jsonify({'status':email+' not found'}), 200)
        except:
            abort(500) # Nondescript server error
        finally:
            cursor.close()
            dbConnection.close()

        if email == session['email']:
            session.pop('email',None)
            session.pop('admin_status',None)

        return make_response(jsonify({'status':'deleted '+email}), 200)
Ejemplo n.º 8
0
    def post(self, email):
        # signin
        # curl -i -H "Content-Type: application/json" -X POST -d '{"username": "******", "password": "******"}' -c cookie-jar -b cookie-jar -k https://cs3103.cs.unb.ca:5045/signin
        # create account
        # curl -i -H "Content-Type: application/json" -X POST -d '{"email": "tshutty"}' -c cookie-jar -b cookie-jar -k https://cs3103.cs.unb.ca:5045/users
        # create folder
        # curl -i -H "Content-Type: application/json" -X POST -d '{"folder_name": "hotdogs","folder_description":"pink", "parent":0}' -c cookie-jar -b cookie-jar -k https://cs3103.cs.unb.ca:5045/users/[email protected]/folders
        # create file
        # curl -i -X POST --form file_description="5:00" --form parent=4 --form "[email protected]" -c cookie-jar -b cookie-jar -k https://cs3103.cs.unb.ca:5045/users/[email protected]/files

        if (settings.APP_HOST == '127.0.0.1'):
            #     with open('session.json') as f:
            #         session = json.load(f)
            session = settings.SESSION
        else:
            from flask import session
        if 'email' not in session:
            return make_response(jsonify({'status': 'not logged in'}), 403)

        check_if_admin()
        # if(settings.APP_HOST == '127.0.0.1'):
        #     with open('session.json') as f:
        #         session = json.load(f)
        if session['email'] != email and session['admin_status'] == 0:
            return make_response(
                jsonify(
                    {'status':
                     'not logged in as ' + email + ' and not admin'}), 403)

        print('form=', request.form)
        print('file=', request.files['file'])
        if (not request.form or not 'file_description' in request.form
                or not 'parent' in request.form or not 'file' in request.files
                or request.files['file'] == ''
                or not allowed_file(request.files['file'].filename)):
            return make_response(jsonify({'status': 'invalid request body'}),
                                 400)

        # file_name = request.form['file_name']
        file_description = request.form['file_description']
        parent = request.form['parent']

        parse = reqparse.RequestParser()
        parse.add_argument('file',
                           type=werkzeug.datastructures.FileStorage,
                           location='files')
        args = parse.parse_args()
        audio_file = args['file']
        file_name = audio_file.filename
        print(audio_file)

        # file = request.files['file']
        # print(file)

        # filename = secure_filename(file.filename)
        # file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

        dbConnection = pymysql.connect(host=settings.MYSQL_HOST,
                                       user=settings.MYSQL_USER,
                                       passwd=settings.MYSQL_PASSWD,
                                       db=settings.MYSQL_DB,
                                       charset='utf8mb4',
                                       cursorclass=pymysql.cursors.DictCursor)

        sql = 'addFile'
        try:
            cursor = dbConnection.cursor()
            sqlArgs = (file_name, file_description, parent, email
                       )  # Must be a collection
            cursor.callproc(sql, sqlArgs)  # stored procedure, with arguments
            file_id = cursor.fetchone()
            dbConnection.commit()  # database was modified, commit the changes
        except Exception as e:
            print(str(e))
            return make_response(jsonify({'status': str(e)}), 400)
        except:
            abort(500)  # Nondescript server error
        finally:
            cursor.close()
            dbConnection.close()
        uri = 'https://' + settings.APP_HOST
        if (settings.APP_HOST != 'tyeshutty.tk'):
            uri = uri + ':' + str(settings.APP_PORT)
        uri = uri + '/users/' + email + '/' + str(file_id['LAST_INSERT_ID()'])

        audio_file.save(
            os.path.join(settings.UPLOAD_FOLDER,
                         str(file_id['LAST_INSERT_ID()'])))

        return make_response(jsonify({"uri": uri}),
                             201)  # successful resource creation
Ejemplo n.º 9
0
    def patch(self, email, file):
        if (settings.APP_HOST == '127.0.0.1'):
            #     with open('session.json') as f:
            #         session = json.load(f)
            session = settings.SESSION
        else:
            from flask import session
        if 'email' not in session:
            return make_response(jsonify({'status': 'not logged in'}), 403)

        check_if_admin()
        # if(settings.APP_HOST == '127.0.0.1'):
        #     with open('session.json') as f:
        #         session = json.load(f)
        if session['email'] != email and session['admin_status'] == 0:
            return make_response(
                jsonify(
                    {'status':
                     'not logged in as ' + email + ' and not admin'}), 403)

        print("fetch req=", request)
        if (not request.json or not 'name' in request.json
                or not 'description' in request.json
                or not 'plays_count' in request.json
                or not 'last_played' in request.json
                or not 'parent' in request.json):
            return make_response(jsonify({'status': 'no request'}), 400)

        name = request.json['name']
        description = request.json['description']
        plays_count = request.json['plays_count']
        last_played = request.json['last_played']
        parent = request.json['parent']

        dbConnection = pymysql.connect(host=settings.MYSQL_HOST,
                                       user=settings.MYSQL_USER,
                                       passwd=settings.MYSQL_PASSWD,
                                       db=settings.MYSQL_DB,
                                       charset='utf8mb4',
                                       cursorclass=pymysql.cursors.DictCursor)
        # First check if parent folder is valid

        sql = 'getFile'
        try:
            cursor = dbConnection.cursor()
            cursor.callproc(sql, [file])  # stored procedure, arguments
            row = cursor.fetchone()
            dbConnection.commit()
        except:
            abort(500)  # Nondescript server error
        finally:
            cursor.close()
        print('fetch row=', row)

        if (row is None):
            dbConnection.close()
            return make_response(jsonify({'status': 'No file'}), 404)
        if (row['owner_email'] == session['email']
                or session['admin_status'] == 1):
            sql = 'setFile'
            try:
                cursor = dbConnection.cursor()
                print('data=', file, name, description, plays_count,
                      last_played, parent, email)
                cursor.callproc(sql, [
                    file, name, description, plays_count, last_played, parent,
                    email
                ])
                dbConnection.commit()  #NEEDED for updates and inserts
            except Exception as e:
                print(e)
                return make_response(jsonify({'status': str(e)}), 200)
            except:
                abort(500)  # Nondescript server error
            finally:
                cursor.close()
                dbConnection.close()

            return make_response(jsonify({'status': 'file updated'}), 204)
        else:
            dbConnection.close()
            return make_response(jsonify({'status': 'not owner'}), 403)
Ejemplo n.º 10
0
    def delete(self, email, file):
        if (settings.APP_HOST == '127.0.0.1'):
            #     with open('session.json') as f:
            #         session = json.load(f)
            session = settings.SESSION
        else:
            from flask import session
        if 'email' not in session:
            return make_response(jsonify({'status': 'not logged in'}), 403)

        check_if_admin()
        # if(settings.APP_HOST == '127.0.0.1'):
        #     with open('session.json') as f:
        #         session = json.load(f)
        if session['email'] != email and session['admin_status'] == 0:
            return make_response(
                jsonify(
                    {'status':
                     'not logged in as ' + email + ' and not admin'}), 403)

        dbConnection = pymysql.connect(host=settings.MYSQL_HOST,
                                       user=settings.MYSQL_USER,
                                       passwd=settings.MYSQL_PASSWD,
                                       db=settings.MYSQL_DB,
                                       charset='utf8mb4',
                                       cursorclass=pymysql.cursors.DictCursor)

        sql = 'getFile'
        try:
            cursor = dbConnection.cursor()
            cursor.callproc(sql, [file])  # stored procedure, arguments
            row = cursor.fetchone()
        except:
            abort(500)  # Nondescript server error
        finally:
            cursor.close()
        print(row)
        if (row is None):
            dbConnection.close()
            return make_response(jsonify({'status': 'No file'}), 200)
        if (row['owner_email'] == session['email']
                or session['admin_status'] == 1):
            sql = 'deleteFile'
            try:
                cursor = dbConnection.cursor()
                cursor.callproc(sql, [file])
                dbConnection.commit()  #NEEDED for updates and inserts
            except Exception as e:
                print(str(e))
                return make_response(jsonify({'status': 'file not deleted'}),
                                     400)
            except:
                abort(500)  # Nondescript server error
            finally:
                cursor.close()
                dbConnection.close()

            if os.path.exists(safe_join(settings.UPLOAD_FOLDER, str(file))):
                os.remove(safe_join(settings.UPLOAD_FOLDER, str(file)))

            return make_response(jsonify({'status': 'deleted ' + str(file)}),
                                 204)
        else:
            dbConnection.close()
            return make_response(jsonify({'status': 'not owner'}), 403)
Ejemplo n.º 11
0
    def patch(self, email, folder):
        if (settings.APP_HOST == '127.0.0.1'):
            #     with open('session.json') as f:
            #         session = json.load(f)
            session = settings.SESSION
        else:
            from flask import session

        if 'email' not in session:
            return make_response(jsonify({'status': 'not logged in'}), 403)

        check_if_admin()
        # if(settings.APP_HOST == '127.0.0.1'):
        #     with open('session.json') as f:
        #         session = json.load(f)
        if session['email'] != email and session['admin_status'] == 0:
            return make_response(
                jsonify(
                    {'status':
                     'not logged in as ' + email + ' and not admin'}), 403)

        if (not request.json or not 'name' in request.json
                or not 'description' in request.json):
            return make_response(jsonify({'status': 'no request'}), 400)

        name = request.json['name']
        description = request.json['description']

        dbConnection = pymysql.connect(host=settings.MYSQL_HOST,
                                       user=settings.MYSQL_USER,
                                       passwd=settings.MYSQL_PASSWD,
                                       db=settings.MYSQL_DB,
                                       charset='utf8mb4',
                                       cursorclass=pymysql.cursors.DictCursor)

        sql = 'getFolder'
        try:
            cursor = dbConnection.cursor()
            cursor.callproc(sql, [folder])  # stored procedure, arguments
            row = cursor.fetchone()
        except:
            abort(500)  # Nondescript server error
        finally:
            cursor.close()
        print(row)

        if row is None:
            return make_response(jsonify({'status': 'no folder'}), 404)
        if (row['owner_email'] == session['email']
                or session['admin_status'] == 1):
            sql = 'setFolder'
            try:
                cursor = dbConnection.cursor()
                cursor.callproc(sql, [folder, name, description])
                dbConnection.commit()  #NEEDED for updates and inserts
            except pymysql.err.InternalError as e:
                return make_response(jsonify({'status': str(e)}), 200)
            except:
                abort(500)  # Nondescript server error
            finally:
                cursor.close()
                dbConnection.close()

            return make_response(jsonify({'status': 'folder updated'}), 204)
        else:
            dbConnection.close()
            return make_response(
                jsonify({'status': 'not owner and not admin'}), 403)
Ejemplo n.º 12
0
    def post(self, email):
        # signin
        # curl -i -H "Content-Type: application/json" -X POST -d '{"username": "******", "password": "******"}' -c cookie-jar -b cookie-jar -k https://cs3103.cs.unb.ca:5045/signin
        # create account
        # curl -i -H "Content-Type: application/json" -X POST -d '{"email": "tshutty"}' -c cookie-jar -b cookie-jar -k https://cs3103.cs.unb.ca:5045/users
        # create folder
        # curl -i -H "Content-Type: application/json" -X POST -d '{"folder_name": "hotdogs","folder_description":"pink", "parent":0}' -c cookie-jar -b cookie-jar -k https://cs3103.cs.unb.ca:5045/users/[email protected]/folders
        # create subfolder
        # curl -i -H "Content-Type: application/json" -X POST -d '{"folder_name": "hotdogs","folder_description":"purple","parent":4}' -c cookie-jar -b cookie-jar -k https://cs3103.cs.unb.ca:5045/users/[email protected]/folders

        if (settings.APP_HOST == '127.0.0.1'):
            #     with open('session.json') as f:
            #         session = json.load(f)
            session = settings.SESSION
        else:
            from flask import session

        if 'email' not in session:
            return make_response(jsonify({'status': 'not logged in'}), 403)

        check_if_admin()
        if session['email'] != email and session['admin_status'] == 0:
            return make_response(
                jsonify(
                    {'status':
                     'not logged in as ' + email + ' and not admin'}), 403)

        if (not request.json or not 'folder_name' in request.json
                or not 'folder_description' in request.json
                or not 'parent' in request.json):
            return make_response(jsonify({'status': 'invalid request body'}),
                                 400)

        folder_name = request.json['folder_name']
        folder_description = request.json['folder_description']
        parent = request.json['parent']
        print("request=", folder_name, folder_description, parent, email)

        dbConnection = pymysql.connect(host=settings.MYSQL_HOST,
                                       user=settings.MYSQL_USER,
                                       passwd=settings.MYSQL_PASSWD,
                                       db=settings.MYSQL_DB,
                                       charset='utf8mb4',
                                       cursorclass=pymysql.cursors.DictCursor)

        sql = 'addFolder'
        try:
            cursor = dbConnection.cursor()
            sqlArgs = (folder_name, folder_description, parent, email
                       )  # Must be a collection
            cursor.callproc(sql, sqlArgs)  # stored procedure, with arguments
            folder_id = cursor.fetchone()
            dbConnection.commit()  # database was modified, commit the changes
        except Exception as e:
            print(str(e))
            return make_response(
                jsonify({'status': 'no such owned parent folder'}), 400)
        except:
            abort(500)  # Nondescript server error
        finally:
            cursor.close()
            dbConnection.close()
        print('folder_id=', folder_id)

        uri = 'https://' + settings.APP_HOST
        if (settings.APP_HOST != 'tyeshutty.tk'):
            uri = uri + ':' + str(settings.APP_PORT)
        uri = uri + '/users/' + email + '/' + str(
            folder_id['LAST_INSERT_ID()'])
        return make_response(jsonify({"uri": uri}),
                             201)  # successful resource creation