Skip to content

Commit

Permalink
Version 0.1.1: Image preview added
Browse files Browse the repository at this point in the history
  • Loading branch information
lepigocher committed Aug 20, 2016
1 parent 9b78408 commit 4bc65ac
Show file tree
Hide file tree
Showing 14 changed files with 254 additions and 34 deletions.
18 changes: 18 additions & 0 deletions App_LocalResources/Settings.ascx.resx
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,22 @@
<data name="lblShowIcon.Text" xml:space="preserve">
<value>Show Icon:</value>
</data>
<data name="lblImagePreview.Help" xml:space="preserve">
<value>Check to allow image preview. This features can consume CPU resources when a lot of visitors use it, because the image is resized on the fly.</value>
</data>
<data name="lblImagePreview.Text" xml:space="preserve">
<value>Image Preview:</value>
</data>
<data name="lblThumbnailHeight.Help" xml:space="preserve">
<value>Max height in pixel of the thumbnail when image preview is enabled.</value>
</data>
<data name="lblThumbnailHeight.Text" xml:space="preserve">
<value>Thumbnail Height:</value>
</data>
<data name="lblThumbnailWidth.Help" xml:space="preserve">
<value>Max width in pixel of the thumbnail when image preview is enabled.</value>
</data>
<data name="lblThumbnailWidth.Text" xml:space="preserve">
<value>Thumbnail Width:</value>
</data>
</root>
9 changes: 9 additions & 0 deletions App_LocalResources/SharedResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,13 @@
<data name="ItemSelectAll.Text" xml:space="preserve">
<value>Select all</value>
</data>
<data name="ImagePreview.Text" xml:space="preserve">
<value>Preview</value>
</data>
<data name="DialogClose.Text" xml:space="preserve">
<value>Close</value>
</data>
<data name="ImagePreviewTitle.Text" xml:space="preserve">
<value>Image preview</value>
</data>
</root>
1 change: 1 addition & 0 deletions Common/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public sealed class Options
public bool SynchronizeFolder { get; set; }
public bool FileManagement { get; set; }
public bool OpenOnDblclick { get; set; }
public bool ImagePreview { get; set; }
}
}
63 changes: 62 additions & 1 deletion Components/ServicesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public HttpResponseMessage Options()
Columns = GetColumns(settings),
SynchronizeFolder = IsAdmin,
FileManagement = settings.FileManagement,
OpenOnDblclick = settings.OpenOnDblclick
OpenOnDblclick = settings.OpenOnDblclick,
ImagePreview = settings.ImagePreview
};

return Request.CreateResponse(HttpStatusCode.OK, options, GetFormatter());
Expand Down Expand Up @@ -164,7 +165,48 @@ public HttpResponseMessage DownloadURL([FromUri] ItemCommand itemCommand)
string url = Globals.LinkClick(string.Format("FileID={0}", file.FileId), -1, -1, false, forceDownload);

return Request.CreateResponse(HttpStatusCode.OK, url);
}

return Request.CreateResponse(HttpStatusCode.NotFound, filePath);
}
catch
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, LocalizeString("UnattentedError"));
}
}

/// <summary>
/// Get image thumbnail.
/// </summary>
/// <returns>Options</returns>
[HttpGet]
[ValidateAntiForgeryToken]
[SupportedModules("TidyModules.DocumentExplorer")]
[DnnAuthorize]
public HttpResponseMessage Thumbnail([FromUri] ItemCommand itemCommand)
{
try
{
string folderPath = PathUtils.Instance.FormatFolderPath(itemCommand.Path);
string filePath = folderPath + itemCommand.Files[0];
IFileInfo file = FileManager.Instance.GetFile(PortalSettings.PortalId, filePath);

if (file != null)
{
DocumentSettings settings = new DocumentSettings(ActiveModule);
int height = settings.ThumbnailHeight;
int width = settings.ThumbnailWidth;
string extension = "." + file.Extension;

using (Stream content = FileManager.Instance.GetFileContent(file))
{
using (Stream thumbnail = ImageUtils.CreateImage(content, height, width, extension))
{
string img64 = string.Format("data:{0};base64,{1}", file.ContentType, ReadFullyAsBase64(thumbnail));

return Request.CreateResponse(HttpStatusCode.OK, img64);
}
}
}

return Request.CreateResponse(HttpStatusCode.NotFound, filePath);
Expand Down Expand Up @@ -522,8 +564,10 @@ private Dictionary<string, string> GetResources()
resources.Add("renameFolderTitle", LocalizeString("RenameFolderTitle"));
resources.Add("renameFileTitle", LocalizeString("RenameFileTitle"));
resources.Add("packNameTitle", LocalizeString("PackNameTitle"));
resources.Add("imagePreviewTitle", LocalizeString("ImagePreviewTitle"));
resources.Add("dialogOk", LocalizeString("DialogOk"));
resources.Add("dialogCancel", LocalizeString("DialogCancel"));
resources.Add("dialogClose", LocalizeString("DialogClose"));
resources.Add("deleteFolderMessage", LocalizeString("DeleteFolderMessage"));
resources.Add("deleteFilesMessage", LocalizeString("DeleteFilesMessage"));
resources.Add("itemSelectAll", LocalizeString("ItemSelectAll"));
Expand All @@ -534,6 +578,7 @@ private Dictionary<string, string> GetResources()
resources.Add("cantPaste", LocalizeString("CantPaste"));
resources.Add("itemRename", LocalizeString("ItemRename"));
resources.Add("itemDelete", LocalizeString("ItemDelete"));
resources.Add("imagePreview", LocalizeString("ImagePreview"));
resources.Add("fileOpen", LocalizeString("FileOpen"));
resources.Add("fileDownload", LocalizeString("FileDownload"));
resources.Add("fileClipboard", LocalizeString("FileClipboard"));
Expand Down Expand Up @@ -833,6 +878,22 @@ private string ZipFiles(IEnumerable<string> zipFileList, IFolderInfo folder, str
return error;
}

/// <summary>
/// Convert content from a stream to a base 64 string.
/// </summary>
/// <param name="input">Stream</param>
/// <returns>Base 64 string</returns>
private string ReadFullyAsBase64(Stream input)
{
using (MemoryStream ms = new MemoryStream())
{
input.Position = 0;
input.CopyTo(ms);

return Convert.ToBase64String(ms.ToArray());
}
}

#endregion
}
}
3 changes: 0 additions & 3 deletions DocumentExplorer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,6 @@
<ItemGroup>
<Content Include="App_LocalResources\Settings.ascx.resx" />
</ItemGroup>
<ItemGroup>
<Content Include="Package\TidyModules.DocumentExplorer_00.01.00_Install.zip" />
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
Expand Down
9 changes: 9 additions & 0 deletions DocumentSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ public DocumentSettings(ModuleInfo module) : base(module)
[ModuleSetting("OpenOnDblclick", "false")]
public bool OpenOnDblclick { get; set; }

[ModuleSetting("ImagePreview", "true")]
public bool ImagePreview { get; set; }

[ModuleSetting("ThumbnailWidth", "450")]
public int ThumbnailWidth { get; set; }

[ModuleSetting("ThumbnailHeight", "300")]
public int ThumbnailHeight { get; set; }

#endregion
}
}
Binary file not shown.
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("0.1.0.0")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyVersion("0.1.1.0")]
[assembly: AssemblyFileVersion("0.1.1.0")]
52 changes: 51 additions & 1 deletion Scripts/explorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
locale = null,
sizes = null,
showIcon = false,
fileManagement = false,
fileManagement = false
imagePreview = false,
hasFilter = false,
resetFilters = false,
dlgWindow = null,
Expand Down Expand Up @@ -282,6 +283,10 @@
options.push(itemTmpl.format("cmfDelete", "fa-trash", locale["itemDelete"]));
}

if (imagePreview) {
options.push(itemTmpl.format("cmfPreview", "fa-eye", locale["imagePreview"]));
}

options.push(itemTmpl.format("cmfOpen", "fa-external-link", locale["fileOpen"]));
options.push(itemTmpl.format("cmfDownload", "fa-download", locale["fileDownload"]));
options.push(itemTmpl.format("cmfClipboard", "fa-link", locale["fileClipboard"]));
Expand Down Expand Up @@ -328,6 +333,14 @@
});
}

// Check if the file is an image
function isImage(extension) {
if (extension == "bmp" || extension == "jpg" || extension == "png" || extension == "gif")
return true;

return false;
}

// Build full url from relative path
function buildURL(relativePath) {
if (relativePath.length > 0 & relativePath[0] != "/") {
Expand Down Expand Up @@ -426,6 +439,39 @@
});
});
break;
case "cmfPreview":
var file = deFiles.puidatatable("getContextMenuSelection");
var cmd = new ItemCommand(folder.path, getFileNames(file));

$.ajax({
type: "GET",
url: services.format("Thumbnail"),
data: cmd,
beforeSend: sf.setModuleHeaders
}).done(function (result, status, xhr) {
var html = "<div><img src='" + result + "' /></div>";

$(html).dialog({
modal: true,
width: "auto",
title: locale["imagePreviewTitle"],
dialogClass: "dnnFormPopup",
buttons: [
{
text: locale["dialogClose"],
click: function () {
$(this).dialog("close");
}
}
],
close: function () {
$(this).dialog("destroy").remove();
}
});
}).fail(function (xhr, result, status) {
showError(status, xhr.responseText);
});
break;
case "cmfOpen":
var file = deFiles.puidatatable("getContextMenuSelection");
var cmd = new ItemCommand(folder.path, getFileNames(file));
Expand Down Expand Up @@ -683,9 +729,12 @@

if (counter > 1) {
cmFiles.find("#cmfRename").addClass("ui-state-disabled");
cmFiles.find("#cmfPreview").addClass("ui-state-disabled");
cmFiles.find("#cmfOpen").addClass("ui-state-disabled");
cmFiles.find("#cmfDownload").addClass("ui-state-disabled");
cmFiles.find("#cmfClipboard").addClass("ui-state-disabled");
} else if (!isImage(data.extension)) {
cmFiles.find("#cmfPreview").addClass("ui-state-disabled");
}
}

Expand All @@ -696,6 +745,7 @@
sizes = locale["sizes"].split(",");
showIcon = options.showIcon;
fileManagement = options.fileManagement;
imagePreview = options.imagePreview;
resetFilters = options.resetFilters;

// Init dialog windows
Expand Down
Loading

0 comments on commit 4bc65ac

Please sign in to comment.