]> khamidou — GitHub mirrors - cagibi.git/commitdiff
added file creation api
authorkarim hamidou <khamidou@gmail.com>
Thu, 12 Jul 2012 07:47:51 +0000 (09:47 +0200)
committerkarim hamidou <khamidou@gmail.com>
Thu, 12 Jul 2012 07:47:51 +0000 (09:47 +0200)
cagibi/server.py

index c8621223180256933c62812635345e3bcda4dd70..d04fc81efd0bee2d5f4b5a5496fa538f225f8531 100644 (file)
@@ -3,14 +3,44 @@ import os, time
 import json
 
 cagibi_folder = "."
+files_info = {}
+
 @route('/folder')
 def filelist():
     """return a list of files with some metadata"""
     dir = {}
+
     for file in os.listdir(cagibi_folder):
        dir[file] = {}
        dir[file]["mtime"] = os.path.getmtime(os.path.join(cagibi_folder, file))
 
     return json.dumps(dir)
 
-run(host='localhost', port=8080)
+@post('/create/')
+def create_file():
+    """Create a file on the server. The method receives two POST parameters:
+        - filename : the filename
+        - contents : the contents
+    """
+    filename = os.path.basename(request.forms.get('filename'))
+    contents = request.forms.get('contents')
+    
+    # FIXME: possible race condition
+    if not os.path.exists(filename) and filename not in files_info:
+        fd = open(filename, "wb")
+        fd.write(contents)
+        fd.close()
+        files_info[filename] = {"rev": 1}
+        save_config(files_info, filename="files.json")
+        return "Ok."
+    else:
+        abort(500, "File already exists")
+
+@post('/update/<filename>')
+def update_filename(filename):
+    """return the deltas corresponding to the file. The client must send in its request the hashes of the file"""  
+    return filename
+
+if __name__ == "__main__":
+    files_info = load_config(filename="files.json")
+    run(host='localhost', port=8080)