Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to show SHA-1 hashes for files in info area #574

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
build_and_deploy:
stage: build
script:
- npm install
- npm run build
- git log --oneline --decorate > ./build/_h5ai/CHANGELOG.txt
- cd ./build/
- rm h5ai-*.zip
- scp -r . [email protected]:/var/www/
environment: staging
10 changes: 9 additions & 1 deletion src/_h5ai/private/conf/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
*/
"info": {
"enabled": true,
"show": false,
"show": true,
"qrcode": true,
"qrFill": "#999",
"qrBack": "#fff"
Expand Down Expand Up @@ -323,6 +323,14 @@
"checkboxes": true
},

/*
Calc the SHA-1 checksum of files.
This operation is real slow for large files.
*/
"sha1": {
"enabled": true
},

/*
Default sort order.
"column" and "reverse" are locally stored.
Expand Down
5 changes: 4 additions & 1 deletion src/_h5ai/private/php/core/class-item.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static function get($context, $path, &$cache) {
public $href;
public $date;
public $size;
public $sha1;
public $is_folder;
public $is_content_fetched;

Expand All @@ -45,14 +46,16 @@ private function __construct($context, $path) {
$this->href = $context->to_href($this->path, $this->is_folder);
$this->date = @filemtime($this->path);
$this->size = Util::filesize($context, $this->path);
$this->sha1 = Util::sha1($context, $this->path);
$this->is_content_fetched = false;
}

public function to_json_object() {
$obj = [
'href' => $this->href,
'time' => $this->date * 1000, // seconds (PHP) to milliseconds (JavaScript)
'size' => $this->size
'size' => $this->size,
'sha1' => $this->sha1
];

if ($this->is_folder) {
Expand Down
34 changes: 34 additions & 0 deletions src/_h5ai/private/php/core/class-sha1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

class SHA1 {
private static $cache = [];

public static function getHash($path) {
$sha1 = new SHA1();
return $sha1->hash($path);
}

public static function getCachedHash($path, $clear_cache = false) {
if (array_key_exists($path, SHA1::$cache) && !$clear_cache) {
return SHA1::$cache[$path];
}

$hash = SHA1::getHash($path, $clear_cache);

SHA1::$cache[$path] = $hash;
return $hash;
}


private function __construct() {

}

private function hash($path) {
if (is_file($path)) {
return sha1_file($path);
}

return null;
}
}
9 changes: 9 additions & 0 deletions src/_h5ai/private/php/core/class-util.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,13 @@ public static function filesize($context, $path) {
$withDu = $context->get_setup()->get('HAS_CMD_DU') && $context->query_option('foldersize.type', null) === 'shell-du';
return Filesize::getCachedSize($path, $withFoldersize, $withDu);
}

public static function sha1($context, $path, $clear_cache = false) {
$sha1Enabled = $context->query_option('sha1.enabled', false);
if($sha1Enabled) {
return SHA1::getCachedHash($path, $clear_cache);
} else {
return null;
}
}
}
5 changes: 2 additions & 3 deletions src/_h5ai/public/css/lib/ext/info.less
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
flex: 0 0 auto;
order: 99;

padding: 32px 32px 32px 48px;
padding: 32px;
white-space: nowrap;
overflow-x: hidden;
width: 240px;
width: 320px;

.icon {
width: 240px;
height: 180px;

img {
Expand Down
10 changes: 10 additions & 0 deletions src/_h5ai/public/js/lib/ext/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const tpl =
<div class="label"></div>
<div class="time"></div>
<div class="size"></div>
<div class="sha1"></div>
<div class="content">
<span class="folders"></span> <span class="l10n-folders"></span>,
<span class="files"></span> <span class="l10n-files"></span>
Expand All @@ -39,6 +40,7 @@ let $img;
let $label;
let $time;
let $size;
let $sha1;
let $content;
let $folders;
let $files;
Expand Down Expand Up @@ -85,6 +87,13 @@ const update = item => {
$size.hide();
}

if(item.sha1) {
$sha1.text(item.sha1 + ' (SHA-1)');
$sha1.show();
} else {
$sha1.hide();
}

if (item.isContentFetched) {
const stats = item.getStats();
$folders.text(stats.folders);
Expand Down Expand Up @@ -131,6 +140,7 @@ const init = () => {
$label = $info.find('.label');
$time = $info.find('.time');
$size = $info.find('.size');
$sha1 = $info.find('.sha1');
$content = $info.find('.content');
$folders = $info.find('.folders');
$files = $info.find('.files');
Expand Down
6 changes: 6 additions & 0 deletions src/_h5ai/public/js/lib/model/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ const getItem = options => {
if (isNum(options.size)) {
item.size = options.size;
}

if(options.sha1) {
item.sha1 = options.sha1;
}

if (options.managed) {
item.isManaged = true;
}
Expand Down Expand Up @@ -120,6 +125,7 @@ const Item = absHref => {
label: createLabel(absHref === '/' ? location.getDomain() : split.name),
time: null,
size: null,
sha1: null,
parent: null,
isManaged: null,
content: {}
Expand Down