mirror of
https://github.com/Jonnyan404/memos-bber.git
synced 2026-08-23 00:41:39 +09:00
fix: 适配 Memos 0.29.x 附件 API
This commit is contained in:
@@ -187,6 +187,7 @@
|
||||
|
||||
function normalizeUploadedItem(entity, fallbackFilename) {
|
||||
if (!entity) return null
|
||||
entity = entity.attachment || entity.resource || entity
|
||||
const inferredId = (function () {
|
||||
const value = entity.id != null ? entity.id : entity.ID != null ? entity.ID : entity.Id
|
||||
if (typeof value === 'number' && Number.isFinite(value)) return Math.floor(value)
|
||||
@@ -204,7 +205,11 @@
|
||||
name: name,
|
||||
filename: entity.filename || fallbackFilename || name,
|
||||
createTime: entity.createTime || entity.createdTs || entity.createdAt,
|
||||
type: entity.type
|
||||
type: entity.type,
|
||||
uid: entity.uid,
|
||||
size: entity.size,
|
||||
memo: entity.memo,
|
||||
externalLink: entity.externalLink
|
||||
}
|
||||
}
|
||||
|
||||
@@ -396,7 +401,7 @@
|
||||
info,
|
||||
payload,
|
||||
function (resp) {
|
||||
const entity = (resp && resp.resource) || resp
|
||||
const entity = (resp && (resp.attachment || resp.resource)) || resp
|
||||
if (success) success(normalizeUploadedItem(entity, nextName))
|
||||
},
|
||||
fail
|
||||
@@ -458,24 +463,35 @@
|
||||
return
|
||||
}
|
||||
|
||||
const uploadedItems = Array.isArray(payload.resourceIdList) ? payload.resourceIdList : []
|
||||
const attachmentReferences = uploadedItems
|
||||
.filter(function (item) {
|
||||
return item && typeof item.name === 'string' && item.name.indexOf('attachments/') === 0
|
||||
})
|
||||
.map(function (item) {
|
||||
return { name: item.name }
|
||||
})
|
||||
const canCreateWithAttachments = flavor === 'modern' && attachmentReferences.length === uploadedItems.length
|
||||
|
||||
requestJson({
|
||||
url: info.apiUrl + 'api/v1/memos',
|
||||
type: 'POST',
|
||||
data: JSON.stringify({
|
||||
content: payload.content,
|
||||
visibility: payload.visibility
|
||||
visibility: payload.visibility,
|
||||
...(canCreateWithAttachments ? { attachments: attachmentReferences } : {})
|
||||
}),
|
||||
contentType: 'application/json',
|
||||
dataType: 'json',
|
||||
headers: { Authorization: 'Bearer ' + info.apiTokens }
|
||||
}, function (data) {
|
||||
const createdName = data && data.name ? data.name : data && data.memo && data.memo.name ? data.memo.name : ''
|
||||
const resources = Array.isArray(payload.resourceIdList) ? payload.resourceIdList : []
|
||||
const resources = uploadedItems
|
||||
if (!createdName) {
|
||||
if (success) success(data)
|
||||
return
|
||||
}
|
||||
if (resources.length === 0) {
|
||||
if (resources.length === 0 || canCreateWithAttachments) {
|
||||
getMemo(createdName, success, fail)
|
||||
return
|
||||
}
|
||||
@@ -497,7 +513,7 @@
|
||||
return {
|
||||
flavor: flavor,
|
||||
needsAuthenticatedImagePreview: function () {
|
||||
return flavor === FLAVOR_V020_V021
|
||||
return true
|
||||
},
|
||||
listTags: listTags,
|
||||
searchMemos: searchMemos,
|
||||
@@ -518,4 +534,4 @@
|
||||
probeFlavor: probeFlavor,
|
||||
resolve: resolve
|
||||
}
|
||||
})(window)
|
||||
})(window)
|
||||
|
||||
@@ -151,10 +151,53 @@
|
||||
return copy
|
||||
}
|
||||
|
||||
function uploadResource() {
|
||||
global.$
|
||||
.ajax({
|
||||
url: urlResources,
|
||||
data: JSON.stringify(payload),
|
||||
type: 'POST',
|
||||
cache: false,
|
||||
processData: false,
|
||||
contentType: 'application/json',
|
||||
dataType: 'json',
|
||||
headers: headers
|
||||
})
|
||||
.done(function (data) {
|
||||
onSuccess(data, 'resources')
|
||||
})
|
||||
.fail(function (xhr) {
|
||||
if (!xhr || xhr.status !== 400) {
|
||||
if (onFail) onFail(xhr)
|
||||
return
|
||||
}
|
||||
|
||||
global.$
|
||||
.ajax({
|
||||
url: urlResources,
|
||||
data: JSON.stringify(stripVisibility(payload)),
|
||||
type: 'POST',
|
||||
cache: false,
|
||||
processData: false,
|
||||
contentType: 'application/json',
|
||||
dataType: 'json',
|
||||
headers: headers
|
||||
})
|
||||
.done(function (data) {
|
||||
onSuccess(data, 'resources')
|
||||
})
|
||||
.fail(function (xhr2) {
|
||||
if (onFail) onFail(xhr2)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// Attachment visibility was removed in Memos v0.26. Send the current
|
||||
// attachment shape first and retain the resource endpoint as a legacy fallback.
|
||||
global.$
|
||||
.ajax({
|
||||
url: urlAttachments,
|
||||
data: JSON.stringify(payload),
|
||||
data: JSON.stringify(stripVisibility(payload)),
|
||||
type: 'POST',
|
||||
cache: false,
|
||||
processData: false,
|
||||
@@ -166,115 +209,11 @@
|
||||
onSuccess(data, 'attachments')
|
||||
})
|
||||
.fail(function (xhr) {
|
||||
if (xhr && xhr.status === 400) {
|
||||
global.$
|
||||
.ajax({
|
||||
url: urlAttachments,
|
||||
data: JSON.stringify(stripVisibility(payload)),
|
||||
type: 'POST',
|
||||
cache: false,
|
||||
processData: false,
|
||||
contentType: 'application/json',
|
||||
dataType: 'json',
|
||||
headers: headers
|
||||
})
|
||||
.done(function (data) {
|
||||
onSuccess(data, 'attachments')
|
||||
})
|
||||
.fail(function (xhrRetry) {
|
||||
if (!isNotFoundLikeXhr(xhrRetry)) {
|
||||
if (onFail) onFail(xhrRetry)
|
||||
return
|
||||
}
|
||||
// fall through to resources below
|
||||
xhr = xhrRetry
|
||||
if (!isNotFoundLikeXhr(xhr)) {
|
||||
if (onFail) onFail(xhr)
|
||||
return
|
||||
}
|
||||
global.$
|
||||
.ajax({
|
||||
url: urlResources,
|
||||
data: JSON.stringify(payload),
|
||||
type: 'POST',
|
||||
cache: false,
|
||||
processData: false,
|
||||
contentType: 'application/json',
|
||||
dataType: 'json',
|
||||
headers: headers
|
||||
})
|
||||
.done(function (data) {
|
||||
onSuccess(data, 'resources')
|
||||
})
|
||||
.fail(function (xhr2) {
|
||||
if (xhr2 && xhr2.status === 400) {
|
||||
global.$
|
||||
.ajax({
|
||||
url: urlResources,
|
||||
data: JSON.stringify(stripVisibility(payload)),
|
||||
type: 'POST',
|
||||
cache: false,
|
||||
processData: false,
|
||||
contentType: 'application/json',
|
||||
dataType: 'json',
|
||||
headers: headers
|
||||
})
|
||||
.done(function (data) {
|
||||
onSuccess(data, 'resources')
|
||||
})
|
||||
.fail(function (xhr3) {
|
||||
if (onFail) onFail(xhr3)
|
||||
})
|
||||
return
|
||||
}
|
||||
if (onFail) onFail(xhr2)
|
||||
})
|
||||
})
|
||||
if (isNotFoundLikeXhr(xhr)) {
|
||||
uploadResource()
|
||||
return
|
||||
}
|
||||
|
||||
if (!isNotFoundLikeXhr(xhr)) {
|
||||
if (onFail) onFail(xhr)
|
||||
return
|
||||
}
|
||||
|
||||
global.$
|
||||
.ajax({
|
||||
url: urlResources,
|
||||
data: JSON.stringify(payload),
|
||||
type: 'POST',
|
||||
cache: false,
|
||||
processData: false,
|
||||
contentType: 'application/json',
|
||||
dataType: 'json',
|
||||
headers: headers
|
||||
})
|
||||
.done(function (data) {
|
||||
onSuccess(data, 'resources')
|
||||
})
|
||||
.fail(function (xhr2) {
|
||||
if (xhr2 && xhr2.status === 400) {
|
||||
global.$
|
||||
.ajax({
|
||||
url: urlResources,
|
||||
data: JSON.stringify(stripVisibility(payload)),
|
||||
type: 'POST',
|
||||
cache: false,
|
||||
processData: false,
|
||||
contentType: 'application/json',
|
||||
dataType: 'json',
|
||||
headers: headers
|
||||
})
|
||||
.done(function (data) {
|
||||
onSuccess(data, 'resources')
|
||||
})
|
||||
.fail(function (xhr3) {
|
||||
if (onFail) onFail(xhr3)
|
||||
})
|
||||
return
|
||||
}
|
||||
if (onFail) onFail(xhr2)
|
||||
})
|
||||
if (onFail) onFail(xhr)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
+11
-4
@@ -480,6 +480,15 @@ function buildV1ResourceStreamUrl(info, resource) {
|
||||
}
|
||||
if (root && !root.endsWith('/')) root += '/'
|
||||
|
||||
if (resource.externalLink) return resource.externalLink
|
||||
|
||||
const name = typeof resource.name === 'string' ? resource.name.trim() : ''
|
||||
const filename = typeof resource.filename === 'string' ? resource.filename : ''
|
||||
if (name.indexOf('attachments/') === 0 && filename) {
|
||||
const url = root + 'file/' + name.split('/').map(encodeURIComponent).join('/') + '/' + encodeURIComponent(filename)
|
||||
return isImageResource(resource) ? url + '?thumbnail=true' : url
|
||||
}
|
||||
|
||||
function isImageResource(r) {
|
||||
if (!r) return false
|
||||
const t = typeof r.type === 'string' ? r.type.toLowerCase() : ''
|
||||
@@ -517,13 +526,11 @@ function buildV1ResourceStreamUrl(info, resource) {
|
||||
if (id) return buildStreamUrl(id)
|
||||
|
||||
// Fallback for older resource shapes.
|
||||
const name = typeof resource.name === 'string' ? resource.name : ''
|
||||
|
||||
// In some memo payloads, the uid may appear as `name` directly.
|
||||
// Example: name="ETU6hjuR..." should map to /o/r/:uid, not /file/:name/:filename.
|
||||
if (isProbablyUid(name)) return buildStreamUrl(name.trim())
|
||||
|
||||
const fileId = resource.publicId || resource.filename
|
||||
const fileId = resource.publicId || filename
|
||||
if (name && fileId) return root + 'file/' + name + '/' + fileId
|
||||
return ''
|
||||
}
|
||||
@@ -1283,4 +1290,4 @@ function sendText(preparedContent) {
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user