Upload new/changed images to GitHub when saving MD

This commit is contained in:
Andrew Miner
2015-06-09 15:41:04 -07:00
parent 4e39b9bb0c
commit 9d0ce8547a
9 changed files with 173 additions and 100 deletions
@@ -6,6 +6,7 @@ All rights reserved.
###
AdsenseController = require './adsense_controller'
EditableFile = require '../models/editable_file'
FullRecipeController = require './full_recipe_controller'
ImageLoader = require './image_loader'
Item = require '../models/item'
@@ -42,7 +43,7 @@ module.exports = class ItemPageController extends PageController
@imageLoader = options.imageLoader
@modPack = options.modPack
@_editingFile = null
@_descriptionFile = null
@_itemSlug = options.itemSlug
@modPack.on Event.change, => @tryRefresh()
@@ -155,13 +156,16 @@ module.exports = class ItemPageController extends PageController
if not @model.item?
return w.reject new Error 'must have an item'
@client.fetchFile path:GitHub.file.itemDescription modSlug:@_itemSlug.mod, itemSlug:@_itemSlug.item
.then (response)=>
@_editingFile = response.json.data
@_editingFile.content = new Buffer(@_editingFile.content, 'base64').toString('utf8')
pathArgs = modSlug:@_itemSlug.mod, itemSlug:@_itemSlug.item
attributes =
fileName: GitHub.file.itemDescription.fileName pathArgs
path: GitHub.file.itemDescription.path pathArgs
if @_editingFile.content.length > 0
@model.item.parse @_editingFile.content
@_descriptionFile = new EditableFile attributes, client:@client
@_descriptionFile.fetch()
.then =>
if @_descriptionFile.encodedData.length > 0
@model.item.parse @_descriptionFile.getDecodedData 'utf8'
else
@model.item.description = ''
@@ -169,15 +173,18 @@ module.exports = class ItemPageController extends PageController
_endEditingDescription: ->
oldDescription = @model.item.description
promises = []
commitMessage = "User-submitted image for #{@model.item.name} from #{global.hostName}"
for imageFile in @_descriptionController.imageFiles
promises.push imageFile.save commitMessage
commitMessage = "User-submitted text for #{@model.item.name} from #{global.hostName}"
@model.item.description = @_descriptionController.model
@_descriptionFile.setDecodedData @model.item.unparse()
promises.push @_descriptionFile.save commitMessage
args =
content: new Buffer(@model.item.unparse(), 'utf8').toString('base64')
message: "User-submitted text for #{@model.item.name}"
path: GitHub.file.itemDescription modSlug:@_itemSlug.mod, itemSlug:@_itemSlug.item
sha: @_editingFile.sha
@client.updateFile args
w.all promises
.catch (e)=>
@model.item.description = oldDescription
throw e
@@ -46,10 +46,7 @@ module.exports = class MarkdownImageController extends BaseController
mimeType = match[1]
encoding = match[2]
@model.set
encodedData: encodedData
status: MarkdownImage.Status.updateable
@model.encodedData = encodedData
logger.info "loaded #{encodedData.length} bytes from #{file.name} as #{mimeType}"
logger.info "starting to read local file: #{file.name}"
@@ -25,7 +25,7 @@ module.exports = class MarkdownImageListController extends BaseController
# Public Methods ###############################################################################
getImageUrlForFile: (fileName)->
@model.getImageUrlForFile fileName
@model.getFile(fileName).imageUrl
reset: ->
@model.reset()
@@ -68,7 +68,7 @@ module.exports = class MarkdownImageListController extends BaseController
super
refresh: ->
@model.loadImages()
@model.fetchImages()
@_controllers ?= []
index = 0
@@ -95,6 +95,5 @@ module.exports = class MarkdownImageListController extends BaseController
return if newValid is oldValid
@_valid = newValid
logger.debug "setting mic.valid to #{@_valid}"
@trigger Event.change + ':valid', this, oldValid, newValid
@trigger Event.change
@@ -104,7 +104,8 @@ module.exports = class MarkdownSectionController extends BaseController
.then =>
@state = State.confirming
.catch (e)=>
logger.error "failed to end editing: #{e}"
message = if e.stack? then e.stack else e
logger.error "failed to end editing: #{message}"
@state = State.appologizing
.delay @confirmDuration
.then =>
@@ -130,6 +131,9 @@ module.exports = class MarkdownSectionController extends BaseController
@trigger Event.change + ':imageBase', this, oldImageBase, newImageBase
@trigger Event.change, this
getImageFiles: ->
return @_imageListController.model.all
getState: ->
return @_state
@@ -143,9 +147,10 @@ module.exports = class MarkdownSectionController extends BaseController
@_updateStateVisibility()
Object.defineProperties @prototype,
editable: {get:@prototype.isEditable}
imageBase: {get:@prototype.getImageBase, set:@prototype.setImageBase}
state: {get:@prototype.getState, set:@prototype.setState}
editable: { get:@prototype.isEditable }
imageBase: { get:@prototype.getImageBase, set:@prototype.setImageBase }
imageFiles: { get:@prototype.getImageFiles }
state: { get:@prototype.getState, set:@prototype.setState }
# BaseController Overrides #####################################################################
@@ -211,7 +216,6 @@ module.exports = class MarkdownSectionController extends BaseController
_updateButtonStates: ->
for $button in [ @$previewButton, @$saveButton ]
logger.debug "updating buttons based upon valid state: #{@_imageListController.valid}"
if @_imageListController.valid
$button.prop 'disabled', false
else