From e5dc9a84869d0c4e695f22327552a824af65ef9f Mon Sep 17 00:00:00 2001
From: Brian Warner <warner@allmydata.com>
Date: Tue, 5 Feb 2008 22:10:22 -0700
Subject: [PATCH] webish: add POST /uri?t=upload&mutable=true

---
 docs/webapi.txt                |  7 +++++++
 src/allmydata/test/test_web.py | 15 +++++++++++++++
 src/allmydata/webish.py        | 20 +++++++++++++++-----
 3 files changed, 37 insertions(+), 5 deletions(-)

diff --git a/docs/webapi.txt b/docs/webapi.txt
index 2526266b..afe35cce 100644
--- a/docs/webapi.txt
+++ b/docs/webapi.txt
@@ -180,6 +180,13 @@ f. uploading a file
    directory, but can be used from an HTML form. The response is the file
    write cap.
 
+  POST http://localhost:8123/uri?t=upload&mutable=true
+
+   This action also uploads a file without attaching it to a virtual drive
+   directory, but creates a mutable file (SSK) instead of an immutable one.
+   The response contains the new URI that was created.
+
+
 g. creating a new directory
 
   PUT http://localhost:8123/uri?t=mkdir
diff --git a/src/allmydata/test/test_web.py b/src/allmydata/test/test_web.py
index 3290e7da..520dcc38 100644
--- a/src/allmydata/test/test_web.py
+++ b/src/allmydata/test/test_web.py
@@ -1006,6 +1006,21 @@ class Web(WebMixin, unittest.TestCase):
         return d
     test_POST_upload_no_link_whendone.todo = "Not yet implemented."
 
+    def test_POST_upload_no_link_mutable(self):
+        d = self.POST("/uri", t="upload", mutable="true",
+                      file=("new.txt", self.NEWFILE_CONTENTS))
+        def _check(uri):
+            uri = uri.strip()
+            u = IURI(uri)
+            self.failUnless(IMutableFileURI.providedBy(u))
+            n = self.s.create_node_from_uri(uri)
+            return n.download_to_data()
+        d.addCallback(_check)
+        def _check2(data):
+            self.failUnlessEqual(data, self.NEWFILE_CONTENTS)
+        d.addCallback(_check2)
+        return d
+
     def test_POST_upload_mutable(self):
         # this creates a mutable file
         d = self.POST(self.public_url + "/foo", t="upload", mutable="true",
diff --git a/src/allmydata/webish.py b/src/allmydata/webish.py
index 7547b3a6..65132317 100644
--- a/src/allmydata/webish.py
+++ b/src/allmydata/webish.py
@@ -1229,11 +1229,20 @@ class URIPOSTHandler(rend.Page):
 
         if t in ("", "upload"):
             # "POST /uri", to create an unlinked file.
-            fileobj = req.fields["file"].file
-            uploadable = FileHandle(fileobj)
-            d = IClient(ctx).upload(uploadable)
-            d.addCallback(lambda results: results.uri)
-            # that fires with the URI of the new file
+            mutable = bool(get_arg(req, "mutable", "").strip())
+            if mutable:
+                # SDMF: files are small, and we can only upload data
+                contents = req.fields["file"]
+                contents.file.seek(0)
+                data = contents.file.read()
+                d = IClient(ctx).create_mutable_file(data)
+                d.addCallback(lambda n: n.get_uri())
+            else:
+                fileobj = req.fields["file"].file
+                uploadable = FileHandle(fileobj)
+                d = IClient(ctx).upload(uploadable)
+                d.addCallback(lambda results: results.uri)
+                # that fires with the URI of the new file
             return d
 
         if t == "mkdir":
@@ -1434,6 +1443,7 @@ class Root(rend.Page):
             "Choose a file: ",
             T.input(type="file", name="file", class_="freeform-input-file"),
             T.input(type="hidden", name="t", value="upload"),
+            " Mutable?:", T.input(type="checkbox", name="mutable"),
             T.input(type="submit", value="Upload!"),
             ]]
         return T.div[form]
-- 
2.45.2