Just fixed it all

Back to this project after over a half-year
This commit is contained in:
Masahiko AMANO 2021-12-08 15:58:48 +03:00
parent 31d5b1c0f2
commit fef8f4ec00
5 changed files with 64 additions and 68 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
/files
*.log
*.log
/counter.txt

View File

@ -32,7 +32,8 @@ def huffman(data):
units[c] += 1
else:
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
if len(units) > 2:
@ -53,7 +54,7 @@ def huffman(data):
return codes
def tbl(table):
def nanako_encode(table):
table = ';'.join([f'{k};{table[k]}' for k in table]).split(';')
byts = []
for i in range(len(table)):
@ -69,7 +70,7 @@ def tbl(table):
return byts
def detbl(byts):
def nanako_decode(byts):
dec = []
table = {}
stack = ''
@ -95,7 +96,7 @@ def compress_file(filename):
log.log(f'Original size: {len(data)} bytes.')
log.log('Creating Huffman table...')
hf = huffman(data)
table = tbl(hf)
table = nanako_encode(hf)
log.log('Embedding Huffman table...')
out = []
ln = bin(len(table))[2:] # embed the table
@ -139,7 +140,7 @@ def decompress_file(filename):
break
i += 1
del data[:i + 2]
table = detbl(data[:int(ln, 2)])
table = nanako_decode(data[:int(ln, 2)])
del data[:int(ln, 2)]
data = ''.join(data)
stack = ''

View File

@ -1,12 +1,17 @@
<?php
$mode = $_POST['mode'];
$file = $_FILES['file'];
if (!is_dir(dirname(__file__) . '/files')) {
mkdir(dirname(__file__) . '/files');
if (!is_dir('./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);
$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');
echo json_encode($result);
?>

View File

@ -12,7 +12,7 @@
crossorigin="anonymous"
/>
<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>
<body>
<header>
@ -20,12 +20,7 @@
</header>
<main>
<h2>Compress your files with the Huffman compression!</h2>
<form
id="form"
class="was-validated"
method="post"
enctype="multipart/form-data"
>
<form id="form" class="was-validated" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="mode">Select mode</label>
<select id="mode" class="form-control" id="mode">
@ -42,12 +37,7 @@
</div>
</div>
<div class="form-group">
<input
id="submit"
type="submit"
class="btn btn-primary"
value="HuffPress!"
/>
<input id="submit" type="submit" class="btn btn-primary" value="HuffPress!" />
</div>
</form>
</main>

View File

@ -1,55 +1,55 @@
function winsize() {
if (
(!toggled && $(window).width() / $(window).height() > 90 / 31) ||
(toggled && $(window).width() / ($(window).height() - 219) > 18 / 7)
(!toggled && $(window).width() / $(window).height() > 90/31) ||
(toggled && $(window).width() / ($(window).height()-219) > 18/7)
) {
$('body').css('justify-content', 'flex-start')
$('body').css('justify-content', 'flex-start');
} else {
$('body').css('justify-content', 'center')
$('body').css('justify-content', 'center');
}
}
window.onload = function () {
};
$(window).on('load', function () {
setTimeout(() => {
$('main').slideDown(500)
}, 100)
winsize()
}
$('main').slideDown(500);
}, 100);
winsize();
});
$(window).on('resize', function () {
winsize()
})
winsize();
});
var toggled = false
var toggled = false;
$('h2').click(function () {
var time = 300
var time = 300;
if (toggled) {
$('form').slideUp(time)
$('main').css('min-height', '9vw')
$('form').slideUp(time);
$('main').css('min-height', '9vw');
setTimeout(() => {
$('h2').css('border-bottom', '0')
}, time)
toggled = false
winsize()
$('h2').css('border-bottom', '0');
}, time);
toggled = false;
winsize();
} else {
$('main').css('min-height', 'calc(9vw + 260px)')
$('form').slideDown(time)
$('form').css('display', 'flex')
$('h2').css('border-bottom', '1px solid black')
toggled = true
$('main').css('min-height', 'calc(9vw + 260px)');
$('form').slideDown(time);
$('form').css('display', 'flex');
$('h2').css('border-bottom', '1px solid black');
toggled = true;
setTimeout(() => {
winsize()
}, time)
}
})
winsize();
}, time);
};
});
$('form').on('submit', function submit(e) {
e.preventDefault()
$('.wrap').css('display', 'flex')
$('.process').css('display', 'block')
var form = new FormData()
form.append('mode', $('#mode').val())
e.preventDefault();
$('.wrap').css('display', 'flex');
$('.process').css('display', 'block');
var form = new FormData();
form.append('mode', $('#mode').val());
$.each($('#file')[0].files, function (i, file) {
form.append('file', file)
})
form.append('file', file);
});
$.ajax({
url: 'huffpress.php',
type: 'POST',
@ -58,7 +58,6 @@ $('form').on('submit', function submit(e) {
dataType: 'json',
data: form,
success: function (resp) {
console.log(resp)
if (resp.status) {
$('.process').css('display', 'none')
$('.complete').css('display', 'block')
@ -75,14 +74,14 @@ $('form').on('submit', function submit(e) {
} else {
$('.process').css('display', 'none')
$('.error').css('display', 'block')
}
};
},
})
})
});
});
$('.closebtn').click(function () {
$('.wrap').css('display', 'none')
$('.process').css('display', 'none')
$('.error').css('display', 'none')
$('.complete').css('display', 'none')
})
$('.wrap').css('display', 'none');
$('.process').css('display', 'none');
$('.error').css('display', 'none');
$('.complete').css('display', 'none');
});