     )
 
   upload: (title, content) ->
+    if this.file == null
+      this.insert(title, content)
+    else
+      this.update(title, content)
+
+  insert: (title, content) ->
     that = this
-    console.log('upload file')
-
-    console.log(content)
     gapi.client.load('drive', 'v2', ->
       that.gdrive.insert(title, content, (file)->
-        console.log(file)
+        that.file = file
+        )
+    )
+
+  update: (title, content) ->
+    that = this
+    gapi.client.load('drive', 'v2', ->
+      that.gdrive.update(that.file.id, title, content, (file)->
         that.file = file
         )
     )

        # immediate: trueが失敗したときはflaseで再チャレンジ
        # 今度はダイアログが開く
        if (authResult && !authResult.error)
          console.log("immediate true")
          that.authResult = authResult
        else
          console.log("immediate false")
          gapi.auth.authorize({
            'client_id': this.gdrive.CLIENT_ID,
            'scope': this.gdrive.SCOPES,

        console.log(file)
    request.execute(callback)

  update: (fileId, title, content, callback) ->
    console.log('update file')
    multipartRequestBody = this.requestBody(title, content)
    request = gapi.client.request({
      'path': '/upload/drive/v2/files/' + fileId,
      'method': 'PUT',
      'params': {'uploadType': 'multipart'},
      'headers': {
        'Content-Type': 'multipart/mixed; boundary="' +
        this.BOUNDARY + '"'
      },
      'body': multipartRequestBody})
    if (!callback)
      callback = (file) ->
        console.log(file)
    request.execute(callback)

  utf8_to_b64: (str) ->
    window.btoa( unescape(encodeURIComponent( str ) ) )

GDriveModel = Backbone.Model.extend({
  initialize: ()->
