Skip to content
Snippets Groups Projects
Commit 6516b469 authored by Jared Hancock's avatar Jared Hancock
Browse files

draft: Put inline image draft race to rest

The solution is simple: upload the image, create the draft automatically,
attach the image to the draft, return the draft_id to the client. That's it.
There's no more need to create the draft in advance now.
parent 4137e304
No related branches found
No related tags found
No related merge requests found
...@@ -33,6 +33,7 @@ $dispatcher = patterns('', ...@@ -33,6 +33,7 @@ $dispatcher = patterns('',
url_post('^(?P<id>\d+)$', 'updateDraftClient'), url_post('^(?P<id>\d+)$', 'updateDraftClient'),
url_delete('^(?P<id>\d+)$', 'deleteDraftClient'), url_delete('^(?P<id>\d+)$', 'deleteDraftClient'),
url_post('^(?P<id>\d+)/attach$', 'uploadInlineImageClient'), url_post('^(?P<id>\d+)/attach$', 'uploadInlineImageClient'),
url_post('^(?P<namespace>[\w.]+)/attach$', 'uploadInlineImageEarlyClient'),
url_get('^(?P<namespace>[\w.]+)$', 'getDraftClient'), url_get('^(?P<namespace>[\w.]+)$', 'getDraftClient'),
url_post('^(?P<namespace>[\w.]+)$', 'createDraftClient') url_post('^(?P<namespace>[\w.]+)$', 'createDraftClient')
)), )),
......
...@@ -111,6 +111,8 @@ class DraftAjaxAPI extends AjaxController { ...@@ -111,6 +111,8 @@ class DraftAjaxAPI extends AjaxController {
echo JsonDataEncoder::encode(array( echo JsonDataEncoder::encode(array(
'content_id' => 'cid:'.$f->getKey(), 'content_id' => 'cid:'.$f->getKey(),
// Return draft_id to connect the auto draft creation
'draft_id' => $draft->getId(),
'filelink' => sprintf('image.php?h=%s', $f->getDownloadHash()) 'filelink' => sprintf('image.php?h=%s', $f->getDownloadHash())
)); ));
} }
...@@ -123,7 +125,7 @@ class DraftAjaxAPI extends AjaxController { ...@@ -123,7 +125,7 @@ class DraftAjaxAPI extends AjaxController {
Http::response(403, "Valid session required"); Http::response(403, "Valid session required");
$vars = array( $vars = array(
'staff_id' => ($thisclient) ? $thisclient->getId() : 0, 'staff_id' => ($thisclient) ? $thisclient->getId() : 1<<31,
'namespace' => $namespace, 'namespace' => $namespace,
); );
...@@ -208,6 +210,22 @@ class DraftAjaxAPI extends AjaxController { ...@@ -208,6 +210,22 @@ class DraftAjaxAPI extends AjaxController {
return self::_uploadInlineImage($draft); return self::_uploadInlineImage($draft);
} }
function uploadInlineImageEarlyClient($namespace) {
global $thisclient;
if (!$thisclient && substr($namespace, -12) != substr(session_id(), -12))
Http::response(403, "Valid session required");
$draft = Draft::create(array(
'staff_id' => ($thisclient) ? $thisclient->getId() : 1<<31,
'namespace' => $namespace,
));
if (!$draft->save())
Http::response(500, 'Unable to create draft');
return $this->uploadInlineImageClient($draft->getId());
}
// Staff interface for drafts ======================================== // Staff interface for drafts ========================================
function createDraft($namespace) { function createDraft($namespace) {
global $thisstaff; global $thisstaff;
...@@ -265,6 +283,22 @@ class DraftAjaxAPI extends AjaxController { ...@@ -265,6 +283,22 @@ class DraftAjaxAPI extends AjaxController {
return self::_uploadInlineImage($draft); return self::_uploadInlineImage($draft);
} }
function uploadInlineImageEarly($namespace) {
global $thisstaff;
if (!$thisstaff)
Http::response(403, "Login required for image upload");
$draft = Draft::create(array(
'staff_id' => $thisstaff->getId(),
'namespace' => $namepace
));
if (!$draft->save())
Http::response(500, 'Unable to create draft');
return $this->uploadInlineImage($draft->getId());
}
function deleteDraft($id) { function deleteDraft($id) {
global $thisstaff; global $thisstaff;
......
...@@ -22,7 +22,7 @@ RedactorPlugins.draft = { ...@@ -22,7 +22,7 @@ RedactorPlugins.draft = {
if (this.opts.draftObjectId) if (this.opts.draftObjectId)
autosave_url += '.' + this.opts.draftObjectId; autosave_url += '.' + this.opts.draftObjectId;
this.opts.autosave = this.opts.autoCreateUrl = autosave_url; this.opts.autosave = this.opts.autoCreateUrl = autosave_url;
this.opts.autosaveInterval = 10; this.opts.autosaveInterval = 30;
this.opts.autosaveCallback = this.afterUpdateDraft; this.opts.autosaveCallback = this.afterUpdateDraft;
this.opts.autosaveErrorCallback = this.autosaveFailed; this.opts.autosaveErrorCallback = this.autosaveFailed;
this.opts.imageUploadErrorCallback = this.displayError; this.opts.imageUploadErrorCallback = this.displayError;
...@@ -33,9 +33,11 @@ RedactorPlugins.draft = { ...@@ -33,9 +33,11 @@ RedactorPlugins.draft = {
'ajax.php/draft/'+this.opts.draftId+'/attach'; 'ajax.php/draft/'+this.opts.draftId+'/attach';
} }
else { else {
// Autosave quickly to and setup image upload in the // Just upload the file. A draft will be created automatically
// afterUpdateDraft callback // and will be configured locally in the afterUpateDraft()
this.opts.autosaveInterval = 1; this.opts.clipboardUploadUrl =
this.opts.imageUpload = this.opts.autoCreateUrl + '/attach';
this.opts.imageUploadCallback = this.afterUpdateDraft;
} }
this.$draft_saved = $('<span>') this.$draft_saved = $('<span>')
...@@ -67,17 +69,14 @@ RedactorPlugins.draft = { ...@@ -67,17 +69,14 @@ RedactorPlugins.draft = {
// If the draft was created, a draft_id will be sent back — update // If the draft was created, a draft_id will be sent back — update
// the URL to send updates in the future // the URL to send updates in the future
if (data.draft_id) { if (!this.opts.draftId && data.draft_id) {
this.opts.draftId = data.draft_id; this.opts.draftId = data.draft_id;
this.opts.autosave = 'ajax.php/draft/' + data.draft_id; this.opts.autosave = 'ajax.php/draft/' + data.draft_id;
this.opts.clipboardUploadUrl = this.opts.clipboardUploadUrl =
this.opts.imageUpload = this.opts.imageUpload =
'ajax.php/draft/'+this.opts.draftId+'/attach'; 'ajax.php/draft/'+this.opts.draftId+'/attach';
if (this.opts.autosaveInterval < 10) { if (!this.get())
clearInterval(this.autosaveInterval); this.set(' ', false);
this.opts.autosaveInterval = 10;
this.autosave();
}
} }
// Only show the [Draft Saved] notice if there is content in the // Only show the [Draft Saved] notice if there is content in the
// field that has been touched // field that has been touched
......
...@@ -155,6 +155,7 @@ $dispatcher = patterns('', ...@@ -155,6 +155,7 @@ $dispatcher = patterns('',
url_post('^(?P<id>\d+)$', 'updateDraft'), url_post('^(?P<id>\d+)$', 'updateDraft'),
url_delete('^(?P<id>\d+)$', 'deleteDraft'), url_delete('^(?P<id>\d+)$', 'deleteDraft'),
url_post('^(?P<id>\d+)/attach$', 'uploadInlineImage'), url_post('^(?P<id>\d+)/attach$', 'uploadInlineImage'),
url_post('^(?P<namespace>[\w.]+)/attach$', 'uploadInlineImageEarly'),
url_get('^(?P<namespace>[\w.]+)$', 'getDraft'), url_get('^(?P<namespace>[\w.]+)$', 'getDraft'),
url_post('^(?P<namespace>[\w.]+)$', 'createDraft'), url_post('^(?P<namespace>[\w.]+)$', 'createDraft'),
url_get('^images/browse$', 'getFileList') url_get('^images/browse$', 'getFileList')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment