|
| 1 | +var shell = new ActiveXObject("WScript.shell") |
| 2 | +var fso = new ActiveXObject("Scripting.FileSystemObject") |
| 3 | +var fsu = DOpus.FSUtil() |
| 4 | +var stt = DOpus.Create().StringTools() |
| 5 | + |
| 6 | +function OnInit(/* ScriptInitData */ data) { |
| 7 | + data.name = "File MIME type column using file.exe" |
| 8 | + data.desc = "Shows the MIME type using the file.exe utility" |
| 9 | + data.default_enable = true |
| 10 | + data.config_desc = DOpus.NewMap() |
| 11 | + data.config_desc("debug") = "Print debug messages to the script log" |
| 12 | + data.config.debug = false |
| 13 | + data.config_desc("fileExeFullName") = "Path to the 'file.exe' utility. You can get this utility by installing 'Git for Windows': https://git-scm.com/download/win" |
| 14 | + data.config.fileExeFullName = "%ProgramFiles%/Git/usr/bin/file.exe" |
| 15 | + data.version = "0.0-dev"; |
| 16 | + data.url = "https://github.com/PolarGoose/DirectoryOpus-TabLabelizer-plugin"; |
| 17 | + |
| 18 | + var cmd = data.AddColumn() |
| 19 | + cmd.name = "File MIME type column using file.exe" |
| 20 | + cmd.method = "OnMimeTypeColumnDataRequested" |
| 21 | + cmd.label = "MIME type" |
| 22 | + cmd.autorefresh = true |
| 23 | + cmd.justify = "right" |
| 24 | + |
| 25 | + var cmd = data.AddColumn() |
| 26 | + cmd.name = "File encoding column using file.exe" |
| 27 | + cmd.method = "OnEncodingColumnDataRequested" |
| 28 | + cmd.label = "Enconding" |
| 29 | + cmd.autorefresh = true |
| 30 | + cmd.justify = "right" |
| 31 | +} |
| 32 | + |
| 33 | +function OnMimeTypeColumnDataRequested(/* ScriptColumnData */ data) { |
| 34 | + var fileFullName = data.item.realpath |
| 35 | + debug("OnFileType: fileFullName=" + fileFullName) |
| 36 | + |
| 37 | + try { |
| 38 | + var fileType = getFileType(fileFullName) |
| 39 | + debug("fileType=" + fileType) |
| 40 | + data.value = fileType |
| 41 | + } catch (e) { |
| 42 | + debug("Exception:" + e) |
| 43 | + data.value = "<error>" |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +function OnEncodingColumnDataRequested(/* ScriptColumnData */ data) { |
| 48 | + var fileFullName = data.item.realpath |
| 49 | + debug("OnFileType: fileFullName=" + fileFullName) |
| 50 | + |
| 51 | + try { |
| 52 | + var fileType = getEncoding(fileFullName) |
| 53 | + debug("fileType=" + fileType) |
| 54 | + data.value = fileType |
| 55 | + } catch (e) { |
| 56 | + debug("Exception:" + e) |
| 57 | + data.value = "<error>" |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +function getFileType(/* Path */ fileFullName) { |
| 62 | + var fileCommandLineArguments = "--mime-type --brief " |
| 63 | + return runFileExeAndReturnOutput(fileFullName, fileCommandLineArguments) |
| 64 | +} |
| 65 | + |
| 66 | +function getEncoding(/* Path */ fileFullName) { |
| 67 | + var fileCommandLineArguments = "--mime --brief " |
| 68 | + var output = runFileExeAndReturnOutput(fileFullName, fileCommandLineArguments) |
| 69 | + if(output === "") { |
| 70 | + return "" |
| 71 | + } |
| 72 | + |
| 73 | + // The output look like: "text/plain; charset=us-ascii". We need to get "us-ascii" |
| 74 | + var match = /charset=([^;\r\n]+)/.exec(output) |
| 75 | + if (match && match.length > 1) { |
| 76 | + return match[1] |
| 77 | + } |
| 78 | + |
| 79 | + throw "Failed to get the text encoding. Output of file.exe: " + output |
| 80 | +} |
| 81 | + |
| 82 | +function runFileExeAndReturnOutput(/* Path */ fileFullName, /* string */ fileCommandLineArguments) { |
| 83 | + // file.exe tool doesn't work for ftp and UNC paths |
| 84 | + if (fileFullName.pathpart.substr(0, 2) === "\\\\" || fileFullName.pathpart.substr(0, 3) === "ftp") { |
| 85 | + debug("Skip UNC or FTP path") |
| 86 | + return ""; |
| 87 | + } |
| 88 | + |
| 89 | + var command = '"' + Script.config.fileExeFullName + '" ' + fileCommandLineArguments + '"' + fileFullName + '"' |
| 90 | + return runCommandAndReturnOutput(command) |
| 91 | +} |
| 92 | + |
| 93 | +function runCommandAndReturnOutput(/* string */ command) { |
| 94 | + var tempFileFullName = fsu.GetTempFilePath() |
| 95 | + var cmdLine = 'cmd.exe /c "' + command + ' > "' + tempFileFullName + '""' |
| 96 | + debug("shell.run " + cmdLine) |
| 97 | + |
| 98 | + try { |
| 99 | + var exitCode = shell.run(cmdLine, 0, true) |
| 100 | + if (exitCode !== 0) { |
| 101 | + throw "Failed to execute the command. ExitCode=" + exitCode |
| 102 | + } |
| 103 | + |
| 104 | + var content = readAllText(tempFileFullName) |
| 105 | + if (content.indexOf("cannot open") === 0) { |
| 106 | + throw "File.exe failed. Output of File.exe: " + content |
| 107 | + } |
| 108 | + |
| 109 | + return content |
| 110 | + } |
| 111 | + finally { |
| 112 | + fso.DeleteFile(tempFileFullName) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +function readAllText(/* string */ fileFullName) { |
| 117 | + var handle = fsu.OpenFile(fileFullName) |
| 118 | + var content = stt.Decode(handle.Read(), "utf8") |
| 119 | + handle.Close() |
| 120 | + return content |
| 121 | +} |
| 122 | + |
| 123 | +function debug(text) { |
| 124 | + if (Script.config.debug) { |
| 125 | + DOpus.Output(text) |
| 126 | + } |
| 127 | +} |
0 commit comments