|
| 1 | +from flask import Flask,url_for, jsonify, redirect, request,render_template |
| 2 | +from flask_cors import CORS |
| 3 | +import os_folder as mycomp |
| 4 | +from ftpconn import FTPOperations |
| 5 | +from sftpconn import SFTPOperations |
| 6 | +from db import DbMyFtp |
| 7 | +from dropboxIslemleri import DropboxOperations |
| 8 | +from drive_conn import DriveService |
| 9 | +import string |
| 10 | + |
| 11 | +app = Flask(__name__) |
| 12 | +CORS(app) # Tüm kaynaklardan gelen isteklere izin verir |
| 13 | + |
| 14 | +servers_dirs = dict() |
| 15 | +serverConn = None |
| 16 | +db = DbMyFtp() |
| 17 | + |
| 18 | +@app.route('/') |
| 19 | +def index(): |
| 20 | + return open('template/index.html', encoding='utf-8').read() |
| 21 | + |
| 22 | +@app.route('/serverconnection.html') |
| 23 | +def serverconnection(): |
| 24 | + return open('template/serverconnection.html', encoding='utf-8').read() |
| 25 | + |
| 26 | +@app.route('/bookmarks.html') |
| 27 | +def openbookmarks(): |
| 28 | + return open('template/bookmarks.html', encoding='utf-8').read() |
| 29 | + |
| 30 | +@app.route('/file_inf.html') |
| 31 | +def openFileInfo(): |
| 32 | + return open('template/file_inf.html', encoding='utf-8').read() |
| 33 | + |
| 34 | +@app.route('/api/fileinfo', methods=['POST']) |
| 35 | +def file_info(): |
| 36 | + global serverConn |
| 37 | + data = request.json |
| 38 | + filepath = data.get('filepath') |
| 39 | + print("geldim") |
| 40 | + print(filepath) |
| 41 | + if not filepath: |
| 42 | + return jsonify({"error": "Filepath is required"}), 400 |
| 43 | + file_info = serverConn.get_file_details(filepath) |
| 44 | + return jsonify(file_info) |
| 45 | + |
| 46 | +@app.route('/api/fileUpdate', methods=['POST']) |
| 47 | +def filemodechange(): |
| 48 | + if request.method == 'POST': |
| 49 | + if request.headers['Content-Type'] == 'application/json': |
| 50 | + data = request.json |
| 51 | + filepath = data.get('filepath') |
| 52 | + filemod = data.get('filemod') |
| 53 | + global serverConn |
| 54 | + serverConn.change_file_mode(filepath, filemod) |
| 55 | + return jsonify({'ISLEM': 'BASARILI'}) |
| 56 | + else: |
| 57 | + return jsonify({'error': 'İçerik tipi "application/json" değil'}) |
| 58 | + |
| 59 | +@app.route('/submit', methods=['POST']) |
| 60 | +def submit(): |
| 61 | + if request.method == 'POST': |
| 62 | + if request.headers['Content-Type'] == 'application/json': |
| 63 | + data = request.json |
| 64 | + conn_type = data.get('conn_type') |
| 65 | + username = data.get('username') |
| 66 | + server = data.get('server') |
| 67 | + port = data.get('port') |
| 68 | + passwd = data.get('passwd') |
| 69 | + sshkey = data.get('sshkey') |
| 70 | + global serverConn |
| 71 | + |
| 72 | + if conn_type == "DropBox": |
| 73 | + try: |
| 74 | + serverConn = DropboxOperations() |
| 75 | + except Exception as e: |
| 76 | + print(e) |
| 77 | + print("Dropbox bağlantısı kurulamadı") |
| 78 | + return jsonify({'error': 'var'}) |
| 79 | + if serverConn.access_token: |
| 80 | + print("Dropbox bağlantısı kuruldu") |
| 81 | + return jsonify({'Bağlantı': 'Başarılı'}) |
| 82 | + return jsonify({'error': 'var'}) |
| 83 | + |
| 84 | + if conn_type == "GoogleDrive": |
| 85 | + try: |
| 86 | + serverConn = DriveService() |
| 87 | + except Exception as e: |
| 88 | + print(e) |
| 89 | + print("Drive bağlantısı kurulamadı") |
| 90 | + return jsonify({'error': 'var'}) |
| 91 | + print("Drive bağlantısı kuruldu") |
| 92 | + return jsonify({'Bağlantı': 'Başarılı'}) |
| 93 | + if username and server and port and passwd or sshkey: |
| 94 | + if conn_type == "FTP": |
| 95 | + try: |
| 96 | + serverConn = FTPOperations(server, int(port), username, passwd) |
| 97 | + except Exception as e: |
| 98 | + print(e) |
| 99 | + return jsonify({'error': 'var'}) |
| 100 | + elif conn_type == "SFTP": |
| 101 | + try: |
| 102 | + serverConn = SFTPOperations(server, int(port), username, passwd) |
| 103 | + except Exception as e: |
| 104 | + print(e) |
| 105 | + return jsonify({'error': 'var'}) |
| 106 | + return jsonify({'Bağlantı': 'Başarılı'}) |
| 107 | + else: |
| 108 | + return jsonify({'Bağlantı': 'Parametreleri Eksik'}) |
| 109 | + else: |
| 110 | + return jsonify({'error': 'İçerik tipi "application/json" değil'}) |
| 111 | + |
| 112 | +@app.route('/api/compdirs', methods=['GET']) |
| 113 | +def getCompDirList(): |
| 114 | + mycompDirlists = mycomp.list_files("C:\\Users\\bilalayakdas\\Desktop\\FtpDirectory") |
| 115 | + return jsonify(mycompDirlists) |
| 116 | + |
| 117 | +@app.route('/api/serverfilelist', methods=['POST']) |
| 118 | +def getDirs(): |
| 119 | + data = request.json |
| 120 | + path = data.get('path') |
| 121 | + if path == None: |
| 122 | + path = "/" |
| 123 | + global serverConn |
| 124 | + global servers_dirs |
| 125 | + |
| 126 | + if serverConn is None: |
| 127 | + return jsonify({"error": "Sunucu bağlantısı kurulmadı"}), 500 |
| 128 | + |
| 129 | + servers_dirs = serverConn.open_folder(path) |
| 130 | + return jsonify(servers_dirs) |
| 131 | + |
| 132 | +@app.route('/api/localfiles', methods=['POST']) |
| 133 | +def localfiles(): |
| 134 | + if request.method == 'POST': |
| 135 | + if request.headers['Content-Type'] == 'application/json': |
| 136 | + data = request.json |
| 137 | + filepath = data.get('filepath') |
| 138 | + mycompDirlists = mycomp.list_files(str(filepath)) |
| 139 | + return jsonify(mycompDirlists) |
| 140 | + |
| 141 | +@app.route('/api/uploadFile', methods=['POST']) |
| 142 | +def uploadFile(): |
| 143 | + try: |
| 144 | + print("uploadFile") |
| 145 | + # JSON içeriğini alalım |
| 146 | + data = request.json |
| 147 | + folderPath = data.get('folderPath') |
| 148 | + fileName = data.get('fileName') |
| 149 | + serverpath = data.get('serverpath') |
| 150 | + serverpath = serverpath +"/"+ fileName |
| 151 | + print(folderPath) |
| 152 | + print(fileName) |
| 153 | + global serverConn |
| 154 | + print(serverpath) |
| 155 | + serverConn.dosya_yukle(serverpath,folderPath.replace("\\","/")+"/"+fileName) |
| 156 | + return jsonify("uploadFile: OK") |
| 157 | + except Exception as e: |
| 158 | + print(e) |
| 159 | + return jsonify("uploadFile: Error") |
| 160 | + |
| 161 | +@app.route('/api/downloadFile', methods=['POST']) |
| 162 | +def downloadFile(): |
| 163 | + try: |
| 164 | + data = request.json |
| 165 | + folderPath = data.get('folderPath') |
| 166 | + fileName = data.get('fileName') |
| 167 | + serverpath = data.get('serverpath') |
| 168 | + serverpath = serverpath +"/"+ fileName |
| 169 | + print(folderPath) |
| 170 | + print(fileName) |
| 171 | + global serverConn |
| 172 | + print(serverpath) |
| 173 | + serverConn.dosya_indir(serverpath,folderPath.replace("\\","/")+"/"+fileName) |
| 174 | + return jsonify("DownoladFile: OK") |
| 175 | + except Exception as e: |
| 176 | + print(e) |
| 177 | + return jsonify("DownoladFile: Error") |
| 178 | + |
| 179 | +@app.route('/api/deleteFileFromServer', methods=['POST']) |
| 180 | +def deleteFileFromServer(): |
| 181 | + try: |
| 182 | + data = request.json |
| 183 | + fileName = data.get('fileName') |
| 184 | + serverpath = data.get('serverpath') |
| 185 | + serverpath = serverpath +"/"+ fileName |
| 186 | + print(fileName) |
| 187 | + global serverConn |
| 188 | + print(serverpath) |
| 189 | + serverConn.dosya_sil(serverpath) |
| 190 | + print("DeleteFile: OK") |
| 191 | + return jsonify("DeleteFile: OK") |
| 192 | + except Exception as e: |
| 193 | + print(e) |
| 194 | + return jsonify("DeleteFile: Error") |
| 195 | + |
| 196 | +@app.route('/api/deleteFileFromLocal', methods=['POST']) |
| 197 | +def deleteFileFromLocal(): |
| 198 | + try: |
| 199 | + data = request.json |
| 200 | + fileName = data.get('fileName') |
| 201 | + folderPath = data.get('folderPath') |
| 202 | + print(fileName) |
| 203 | + print(folderPath) |
| 204 | + mycomp.delete_file(folderPath+"/"+fileName) |
| 205 | + print("DeleteFile: OK") |
| 206 | + return jsonify("DeleteFile: OK") |
| 207 | + except Exception as e: |
| 208 | + print(e) |
| 209 | + return jsonify("DeleteFile: Error") |
| 210 | + |
| 211 | +@app.route('/api/bookmark', methods=['GET']) |
| 212 | +def get_bookmark(): |
| 213 | + bookmark_data = db.selectBookMarks() |
| 214 | + if bookmark_data: |
| 215 | + return jsonify(bookmark_data) |
| 216 | + else: |
| 217 | + return jsonify({"error": "No data found"}), 404 |
| 218 | + |
| 219 | +@app.route('/api/localBookmark', methods=['GET']) |
| 220 | +def get_localbookmark(): |
| 221 | + bookmark_data = db.selectLocalBookMarks() |
| 222 | + if bookmark_data: |
| 223 | + return jsonify(bookmark_data) |
| 224 | + else: |
| 225 | + return jsonify({"error": "No data found"}), 404 |
| 226 | + |
| 227 | +@app.route('/api/getBookmarksList', methods=['GET']) |
| 228 | +def get_allbookmark(): |
| 229 | + bookmark_data = db.selectAllBookMarks() |
| 230 | + if bookmark_data: |
| 231 | + return jsonify(bookmark_data) |
| 232 | + else: |
| 233 | + return jsonify({"error": "No data found"}), 404 |
| 234 | + |
| 235 | +@app.route('/api/saveBookmark', methods=['POST']) |
| 236 | +def save_bookmark(): |
| 237 | + if request.method == 'POST': |
| 238 | + if request.headers['Content-Type'] == 'application/json': |
| 239 | + data = request.json |
| 240 | + conn_name = data.get('CONNECTION_NAME') |
| 241 | + conn_type = data.get('CONNECTION_TYPE') |
| 242 | + server = data.get('HOSTNAME') |
| 243 | + port = data.get('PORT') |
| 244 | + username = data.get('USERNAME') |
| 245 | + passwd = data.get('PASSWORD') |
| 246 | + sshkey = data.get('SSHKEY', None) # SSH key optional |
| 247 | + db.insertBookMarks(conn_name,conn_type, username, server, port, passwd) |
| 248 | + return jsonify({'CEVAP': 'Kayıt başarılı'}) |
| 249 | + |
| 250 | + else: |
| 251 | + return jsonify({'error': 'Parametreler eksik veya yanlış'}) |
| 252 | + else: |
| 253 | + return jsonify({'error': 'İçerik tipi "application/json" değil'}) |
| 254 | + |
| 255 | +@app.route('/api/deleteBookmark', methods=['POST']) |
| 256 | +def delete_bookmark(): |
| 257 | + if request.method == 'POST': |
| 258 | + if request.headers['Content-Type'] == 'application/json': |
| 259 | + data = request.json |
| 260 | + id = data.get('id') |
| 261 | + db.deleteBookMarks(id) |
| 262 | + return jsonify({'CEVAP': 'SİLİNDİ'}) |
| 263 | + |
| 264 | + else: |
| 265 | + return jsonify({'error': 'Parametreler eksik veya yanlış'}) |
| 266 | + else: |
| 267 | + return jsonify({'error': 'İçerik tipi "application/json" değil'}) |
| 268 | + |
| 269 | +if __name__ == '__main__': |
| 270 | + app.run(host='127.0.0.1', port=5502,debug=True) |
0 commit comments