Just fixed it all
Back to this project after over a half-year
This commit is contained in:
parent
31d5b1c0f2
commit
fef8f4ec00
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
/files
|
/files
|
||||||
*.log
|
*.log
|
||||||
|
/counter.txt
|
||||||
11
huffman.py
11
huffman.py
@ -32,7 +32,8 @@ def huffman(data):
|
|||||||
units[c] += 1
|
units[c] += 1
|
||||||
else:
|
else:
|
||||||
units[c] = 1
|
units[c] = 1
|
||||||
units, codes = sorted([([u], units[u]) for u in units], key=lambda u: u[1]), dict.fromkeys(units.keys(), '')
|
codes = dict.fromkeys(units.keys(), '')
|
||||||
|
units = sorted([([u], units[u]) for u in units], key=lambda u: u[1])
|
||||||
|
|
||||||
while units: # creating Haffman table
|
while units: # creating Haffman table
|
||||||
if len(units) > 2:
|
if len(units) > 2:
|
||||||
@ -53,7 +54,7 @@ def huffman(data):
|
|||||||
return codes
|
return codes
|
||||||
|
|
||||||
|
|
||||||
def tbl(table):
|
def nanako_encode(table):
|
||||||
table = ';'.join([f'{k};{table[k]}' for k in table]).split(';')
|
table = ';'.join([f'{k};{table[k]}' for k in table]).split(';')
|
||||||
byts = []
|
byts = []
|
||||||
for i in range(len(table)):
|
for i in range(len(table)):
|
||||||
@ -69,7 +70,7 @@ def tbl(table):
|
|||||||
return byts
|
return byts
|
||||||
|
|
||||||
|
|
||||||
def detbl(byts):
|
def nanako_decode(byts):
|
||||||
dec = []
|
dec = []
|
||||||
table = {}
|
table = {}
|
||||||
stack = ''
|
stack = ''
|
||||||
@ -95,7 +96,7 @@ def compress_file(filename):
|
|||||||
log.log(f'Original size: {len(data)} bytes.')
|
log.log(f'Original size: {len(data)} bytes.')
|
||||||
log.log('Creating Huffman table...')
|
log.log('Creating Huffman table...')
|
||||||
hf = huffman(data)
|
hf = huffman(data)
|
||||||
table = tbl(hf)
|
table = nanako_encode(hf)
|
||||||
log.log('Embedding Huffman table...')
|
log.log('Embedding Huffman table...')
|
||||||
out = []
|
out = []
|
||||||
ln = bin(len(table))[2:] # embed the table
|
ln = bin(len(table))[2:] # embed the table
|
||||||
@ -139,7 +140,7 @@ def decompress_file(filename):
|
|||||||
break
|
break
|
||||||
i += 1
|
i += 1
|
||||||
del data[:i + 2]
|
del data[:i + 2]
|
||||||
table = detbl(data[:int(ln, 2)])
|
table = nanako_decode(data[:int(ln, 2)])
|
||||||
del data[:int(ln, 2)]
|
del data[:int(ln, 2)]
|
||||||
data = ''.join(data)
|
data = ''.join(data)
|
||||||
stack = ''
|
stack = ''
|
||||||
|
|||||||
@ -1,12 +1,17 @@
|
|||||||
<?php
|
<?php
|
||||||
$mode = $_POST['mode'];
|
$mode = $_POST['mode'];
|
||||||
$file = $_FILES['file'];
|
$file = $_FILES['file'];
|
||||||
if (!is_dir(dirname(__file__) . '/files')) {
|
if (!is_dir('./files')) {
|
||||||
mkdir(dirname(__file__) . '/files');
|
mkdir('./files');
|
||||||
}
|
}
|
||||||
$path = dirname(__file__) . '/files/' . basename($file['name']);
|
if (!file_exists('./counter.txt')){
|
||||||
|
file_put_contents('./counter.txt', '0');
|
||||||
|
}
|
||||||
|
$id = (int)file_get_contents('./counter.txt');
|
||||||
|
file_put_contents('./counter.txt', (string)($id+1));
|
||||||
|
$path = "./files/{$id}__".basename($file['name']);
|
||||||
move_uploaded_file($file['tmp_name'], $path);
|
move_uploaded_file($file['tmp_name'], $path);
|
||||||
$result = json_decode((string)shell_exec('python ' . dirname(__file__) . '/huffman.py -' . ($mode == 'compress' ? 'c' : 'd') . ' "' . $path . '"'));
|
$result = json_decode((string)shell_exec(dirname(__file__)."/huffman.py -".($mode == 'compress' ? 'c' : 'd')." \"{$path}\""));
|
||||||
header('Content-Type: application/json');
|
header('Content-Type: application/json');
|
||||||
echo json_encode($result);
|
echo json_encode($result);
|
||||||
?>
|
?>
|
||||||
16
index.html
16
index.html
@ -12,7 +12,7 @@
|
|||||||
crossorigin="anonymous"
|
crossorigin="anonymous"
|
||||||
/>
|
/>
|
||||||
<link rel="stylesheet" href="./style.css" />
|
<link rel="stylesheet" href="./style.css" />
|
||||||
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
|
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<header>
|
<header>
|
||||||
@ -20,12 +20,7 @@
|
|||||||
</header>
|
</header>
|
||||||
<main>
|
<main>
|
||||||
<h2>Compress your files with the Huffman compression!</h2>
|
<h2>Compress your files with the Huffman compression!</h2>
|
||||||
<form
|
<form id="form" class="was-validated" method="post" enctype="multipart/form-data">
|
||||||
id="form"
|
|
||||||
class="was-validated"
|
|
||||||
method="post"
|
|
||||||
enctype="multipart/form-data"
|
|
||||||
>
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="mode">Select mode</label>
|
<label for="mode">Select mode</label>
|
||||||
<select id="mode" class="form-control" id="mode">
|
<select id="mode" class="form-control" id="mode">
|
||||||
@ -42,12 +37,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<input
|
<input id="submit" type="submit" class="btn btn-primary" value="HuffPress!" />
|
||||||
id="submit"
|
|
||||||
type="submit"
|
|
||||||
class="btn btn-primary"
|
|
||||||
value="HuffPress!"
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</main>
|
</main>
|
||||||
|
|||||||
85
script.js
85
script.js
@ -3,53 +3,53 @@ function winsize() {
|
|||||||
(!toggled && $(window).width() / $(window).height() > 90/31) ||
|
(!toggled && $(window).width() / $(window).height() > 90/31) ||
|
||||||
(toggled && $(window).width() / ($(window).height()-219) > 18/7)
|
(toggled && $(window).width() / ($(window).height()-219) > 18/7)
|
||||||
) {
|
) {
|
||||||
$('body').css('justify-content', 'flex-start')
|
$('body').css('justify-content', 'flex-start');
|
||||||
} else {
|
} else {
|
||||||
$('body').css('justify-content', 'center')
|
$('body').css('justify-content', 'center');
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
window.onload = function () {
|
$(window).on('load', function () {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
$('main').slideDown(500)
|
$('main').slideDown(500);
|
||||||
}, 100)
|
}, 100);
|
||||||
winsize()
|
winsize();
|
||||||
}
|
});
|
||||||
$(window).on('resize', function () {
|
$(window).on('resize', function () {
|
||||||
winsize()
|
winsize();
|
||||||
})
|
});
|
||||||
|
|
||||||
var toggled = false
|
var toggled = false;
|
||||||
$('h2').click(function () {
|
$('h2').click(function () {
|
||||||
var time = 300
|
var time = 300;
|
||||||
if (toggled) {
|
if (toggled) {
|
||||||
$('form').slideUp(time)
|
$('form').slideUp(time);
|
||||||
$('main').css('min-height', '9vw')
|
$('main').css('min-height', '9vw');
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
$('h2').css('border-bottom', '0')
|
$('h2').css('border-bottom', '0');
|
||||||
}, time)
|
}, time);
|
||||||
toggled = false
|
toggled = false;
|
||||||
winsize()
|
winsize();
|
||||||
} else {
|
} else {
|
||||||
$('main').css('min-height', 'calc(9vw + 260px)')
|
$('main').css('min-height', 'calc(9vw + 260px)');
|
||||||
$('form').slideDown(time)
|
$('form').slideDown(time);
|
||||||
$('form').css('display', 'flex')
|
$('form').css('display', 'flex');
|
||||||
$('h2').css('border-bottom', '1px solid black')
|
$('h2').css('border-bottom', '1px solid black');
|
||||||
toggled = true
|
toggled = true;
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
winsize()
|
winsize();
|
||||||
}, time)
|
}, time);
|
||||||
}
|
};
|
||||||
})
|
});
|
||||||
|
|
||||||
$('form').on('submit', function submit(e) {
|
$('form').on('submit', function submit(e) {
|
||||||
e.preventDefault()
|
e.preventDefault();
|
||||||
$('.wrap').css('display', 'flex')
|
$('.wrap').css('display', 'flex');
|
||||||
$('.process').css('display', 'block')
|
$('.process').css('display', 'block');
|
||||||
var form = new FormData()
|
var form = new FormData();
|
||||||
form.append('mode', $('#mode').val())
|
form.append('mode', $('#mode').val());
|
||||||
$.each($('#file')[0].files, function (i, file) {
|
$.each($('#file')[0].files, function (i, file) {
|
||||||
form.append('file', file)
|
form.append('file', file);
|
||||||
})
|
});
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: 'huffpress.php',
|
url: 'huffpress.php',
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
@ -58,7 +58,6 @@ $('form').on('submit', function submit(e) {
|
|||||||
dataType: 'json',
|
dataType: 'json',
|
||||||
data: form,
|
data: form,
|
||||||
success: function (resp) {
|
success: function (resp) {
|
||||||
console.log(resp)
|
|
||||||
if (resp.status) {
|
if (resp.status) {
|
||||||
$('.process').css('display', 'none')
|
$('.process').css('display', 'none')
|
||||||
$('.complete').css('display', 'block')
|
$('.complete').css('display', 'block')
|
||||||
@ -75,14 +74,14 @@ $('form').on('submit', function submit(e) {
|
|||||||
} else {
|
} else {
|
||||||
$('.process').css('display', 'none')
|
$('.process').css('display', 'none')
|
||||||
$('.error').css('display', 'block')
|
$('.error').css('display', 'block')
|
||||||
}
|
};
|
||||||
},
|
},
|
||||||
})
|
});
|
||||||
})
|
});
|
||||||
|
|
||||||
$('.closebtn').click(function () {
|
$('.closebtn').click(function () {
|
||||||
$('.wrap').css('display', 'none')
|
$('.wrap').css('display', 'none');
|
||||||
$('.process').css('display', 'none')
|
$('.process').css('display', 'none');
|
||||||
$('.error').css('display', 'none')
|
$('.error').css('display', 'none');
|
||||||
$('.complete').css('display', 'none')
|
$('.complete').css('display', 'none');
|
||||||
})
|
});
|
||||||
Reference in New Issue
Block a user