我们为用户构建了一个自定义前端,可以在 Sitecore 9 上发布线程和上传图片。最近,我们将用户生成的内容移到了它自己的数据库中,因此媒体文件不再位于正确的 sitecore“媒体库”中. 此外,文件存储在网络共享上。上传图片效果很好。紧接着问题出在哪里。
上传图片后,我们的 rest api 从 MediaManager.GetMediaUrl(itemId) 返回媒体 url。这也有效。它返回一个有效的 url,它的格式正确并且应该解析。不幸的是,在一段时间之后,该 url 使 sitecore 跳到我们的 404 页面上跳了 302 秒。
我可以通过内容编辑器查看图像,并且我们的自定义内容处理程序将图像文件夹节点注入到主数据库和 Web 数据库中。
为什么链接管理器可以找到网址,但图片不可用?我昨天下午上传了一些东西,今天早上我检查时,图像现在可以在网站上找到。任何信息或建议表示赞赏。
我曾多次尝试保存图像,希望这可能会触发任何导致图像显示的神秘 Sitecore 事件。由于单独的数据库没有发布,我无法尝试。我删除了所有认为它不能默认为特定语言的版本。没有什么。只有时间似乎使图像可见。下面的代码工作得很好。我只是把它放在这里展示一些工作。
public MediaItem UploadSimpleMedia(MediaGallerySimpleUploadRequest request)
{
try
{
var destinationFolder = request.ParentItemId != null
? _content.GetItem<Item>(request.ParentItemId.Value)
: _content.GetItem<Item>(_publicLibraryPath + "/embeded");
var name = !string.IsNullOrEmpty(request.Name)
? ItemUtil.ProposeValidItemName(request.Name)
: ItemUtil.ProposeValidItemName(request.Files[0].FileName);
var creator = new MediaCreator();
var tags = request.Tags != null ? request.Tags.Split(',') : new string[0];
var tagIds = _tagService.GetTagIds(tags);
var tagIdString = string.Join(",", tagIds);
var options = new MediaCreatorOptions()
{
AlternateText = request.Name,
FileBased = true,
IncludeExtensionInItemName = false,
Versioned = false,
Destination = $"{destinationFolder.Paths.Path}/{name}",
Database = Factory.GetDatabase("content")
};
using (new SecurityDisabler())
using (new DatabaseCacheDisabler())
{
MediaItem item = creator.CreateFromStream(request.Files[0].InputStream, request.Files[0].FileName, options);
item.BeginEdit();
item.InnerItem["Title"] = request.Name;
item.InnerItem["Description"] = request.Description;
item.InnerItem["__Semantics"] = tagIdString;
// Sitecore won't calculate MIME Type or File Size automatically unless you upload directly
// to the Sitecore Media Library. We're uploading in-place for private groups for permission inheritance
// and now because of indexing, they are getting uploaded to /Community/Gallery/Files if not private.
item.InnerItem["Size"] = request.Files[0].ContentLength.ToString();
item.InnerItem["Mime Type"] = request.Files[0].ContentType;
item.EndEdit();
// Just pausing here to reflect on why Sitecore is so sexily complicated
// that it takes time to display an image on the CD, but has no problem giving
// up the media url
CacheManager.GetHtmlCache(Sitecore.Context.Site);
_searchService.RefreshIndex(item.ID.Guid);
var fromDatabase = _content.GetItem<Item>(item.ID.Guid);
return fromDatabase;
}
}
catch (Exception ex)
{
_logger.Error(this.GetType().AssemblyQualifiedName, ex);
_logger.Trace(ex.StackTrace);
}
return null;
}
在我们的自定义日志或默认站点核心日志中,我没有收到任何错误消息。图像只是没有解决。是缓存吗?我什至通过调用 CacheManager.ClearAllCaches() 清除了所有缓存。