Skip to content

Commit f3b1d7b

Browse files
committed
Set proper tags to a file
1 parent 273b591 commit f3b1d7b

File tree

13 files changed

+205
-28
lines changed

13 files changed

+205
-28
lines changed

app/File.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,58 @@ public function fetch_tags() {
115115
}
116116
}
117117

118+
/**
119+
* Write the given tags in the file
120+
*
121+
* @param array $tags
122+
* @return array
123+
*/
124+
public function write_tags(array $tags) {
125+
$textEncoding = 'UTF-8';
126+
$getID3 = new \getID3;
127+
$getID3->setOption(array('encoding' => $textEncoding));
128+
129+
$tagwriter = new \getid3_writetags;
130+
$tagwriter->filename = $this->path;
131+
$tagwriter->tagformats = array('id3v2.3');
132+
133+
// set various options (optional)
134+
$tagwriter->overwrite_tags = true;
135+
$tagwriter->tag_encoding = $textEncoding;
136+
$tagwriter->remove_other_tags = true;
137+
138+
$fileInfo = $getID3->analyze($this->path);
139+
140+
//XXX This will remove all tags
141+
// \getid3_lib::CopyTagsToComments($fileInfo);
142+
// foreach ($fileInfo['tags']['id3v2'] as $key => $value) {
143+
// $tagData[$key] = $value;
144+
// }
145+
146+
// populate data array
147+
$tagData['comment'] = array();
148+
foreach ($tags as $tag => $value) {
149+
$tagData[$tag] = array($value);
150+
}
151+
152+
if (!empty($fileInfo['comments']['picture'])) {
153+
$tagData['attached_picture'][0]['picturetypeid'] = 3;
154+
$tagData['attached_picture'][0]['description'] = 'Cover';
155+
$tagData['attached_picture'][0]['data'] = $fileInfo['comments']['picture'][0]['data'];
156+
$tagData['attached_picture'][0]['mime'] = $fileInfo['comments']['picture'][0]['image_mime'];
157+
}
158+
159+
$tagwriter->tag_data = $tagData;
160+
161+
// write tags
162+
$tagwriter->WriteTags();
163+
164+
return array(
165+
'warnings' => $tagwriter->warnings,
166+
'errors' => $tagwriter->errors
167+
);
168+
}
169+
118170
/**
119171
* Generate a search link using the tags
120172
*

app/Http/Controllers/EditTagController.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace App\Http\Controllers;
44

55
use App\DirReader;
6+
use App\File;
7+
use Illuminate\Http\Request;
68

79
class EditTagController extends Controller {
810
const SAMPLEDIR = 'C:\\Users\\Alex\\Desktop\\Temp\\_sample';
@@ -13,4 +15,16 @@ public function index() {
1315

1416
return view('list_tags', compact('files'));
1517
}
18+
19+
/**
20+
* @param \Illuminate\Http\Request $request
21+
*/
22+
public function tagFile(Request $request) {
23+
$file = new File($request->filepath);
24+
$result = $file->write_tags($request->tags);
25+
26+
return [
27+
'result' => $result,
28+
];
29+
}
1630
}

public/css/app.css

Lines changed: 20 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/css/app.css.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/js/rename.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ function moveAction(element) {
1111
});
1212
}
1313

14-
function deleteAction(element) {
14+
function deleteAction(element, colspan) {
1515
var $this = $(element);
1616
var $row = $this.parents('tr');
1717

1818
makeRequest($row, deleteUrl, function(response) {
1919
if (response.result) {
20-
$row.html('<td colspan="4" class="alert alert-success" role="alert">The file was successfully deleted !</td>');
20+
$row.html('<td colspan="' + colspan + '" class="alert alert-success" role="alert">The file was successfully deleted !</td>');
2121
} else {
22-
$row.html('<td colspan="4" class="alert alert-danger" role="alert">Error while deleting the file</td>');
22+
$row.html('<td colspan="' + colspan + '" class="alert alert-danger" role="alert">Error while deleting the file</td>');
2323
}
2424
});
2525
}

public/js/tags.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**** FUNCTIONS ****/
2+
// When clicking on the "Set" button, set all tags to suggested values
3+
function setAllTags(element) {
4+
var $this = $(element);
5+
var $row = $this.parents('tr');
6+
7+
$row.find('td').has('a.tag-suggestion').each(function() {
8+
$(this).find('input.form-control').val(
9+
$(this).find('a.tag-suggestion:first').text()
10+
);
11+
});
12+
}
13+
14+
// When clicking on the "Tag" button, make the request to update the tags
15+
function tagAction(element) {
16+
var $this = $(element);
17+
var $row = $this.parents('tr');
18+
19+
var filepath = $row.find('.form-filepath').val();
20+
21+
$.post(
22+
tagFileUrl,
23+
{
24+
'filepath': filepath,
25+
'tags' : {
26+
artist: $row.find('.form-artist').val(),
27+
title: $row.find('.form-title').val(),
28+
album: $row.find('.form-album').val(),
29+
band: $row.find('.form-band').val(),
30+
publisher: $row.find('.form-publisher').val(),
31+
genre: $row.find('.form-genre').val(),
32+
year: $row.find('.form-year').val(),
33+
track_number: $row.find('.form-track_number').val(),
34+
bpm: $row.find('.form-bpm').val(),
35+
initial_key: $row.find('.form-initial_key').val(),
36+
},
37+
'_token' : csrf_token
38+
},
39+
function(response) {
40+
if (response.result.errors.length > 0) {
41+
$row.html('<td colspan="13" class="alert alert-danger" role="alert">Error while tagging the file<ul class="errors"></ul></td>');
42+
$(response.result.errors).each(function(i, error) {
43+
$row.find('.errors').append("<li>" + error + "</li>");
44+
});
45+
} else {
46+
$row.html('<td colspan="13" class="alert alert-success" role="alert">The file was successfully tagged !</td>');
47+
if (response.result.warnings) console.log(response.result.warnings);
48+
}
49+
},
50+
'json'
51+
);
52+
}
53+
54+
/**** BEGIN SCRIPT ****/
55+
$(document).ready(function() {
56+
57+
// Show a warning if current value differs from at least one suggested value
58+
$('a.tag-suggestion').each(function() {
59+
$input = $(this).parents('td').find('input.form-control');
60+
if ($(this).text() !== $input.val()) $input.parent().addClass('has-warning');
61+
});
62+
63+
// When clicking on suggestion links, the value of the form will update automatically
64+
$('a.tag-suggestion').click(function(event) {
65+
event.preventDefault();
66+
$(this).parents('td').find('input.form-control').val($(this).text());
67+
});
68+
69+
});

resources/assets/sass/app.scss

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ footer {
4444
height: 75px;
4545

4646
td, th {
47-
vertical-align: middle;
47+
vertical-align: top;
4848

4949
.suggested {
5050
font-size: 10px;
@@ -59,6 +59,7 @@ footer {
5959
}
6060

6161
td.filename {
62+
vertical-align: middle;
6263
padding-left: 15px;
6364
max-width: 300px;
6465
}
@@ -67,6 +68,7 @@ footer {
6768
width: 50px;
6869
padding: 0px;
6970
text-align: center;
71+
vertical-align: middle;
7072
span {
7173
margin: 0px;
7274
padding: 0px;
@@ -76,17 +78,21 @@ footer {
7678
}
7779
}
7880

79-
td.action, td.year, td.track {
80-
width: 50px;
81-
max-width: 60px;
81+
td.year {
82+
width: 70px;
83+
max-width: 80px;
8284
}
8385

84-
td.bpm, td.key {
85-
width: 30px;
86-
max-width: 50px;
86+
td.track, td.bpm, td.key {
87+
width: 60px;
88+
max-width: 70px;
8789
}
8890

8991
td.action {
92+
width: 50px;
93+
max-width: 70px;
94+
text-align: center;
95+
vertical-align: middle;
9096
button {
9197
width: 45px;
9298
}
@@ -95,6 +101,10 @@ footer {
95101
}
96102
}
97103

104+
td.valid-button {
105+
text-align: right;
106+
}
107+
98108
}
99109
}
100110
}

resources/views/helpers/display_tag.blade.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
@if (isset($file->get_tags()[$tag]))
22
@if (count($file->get_tags()[$tag]) === 1)
3-
{{ array_first($file->get_tags()[$tag]) }}
3+
<input type="text" name="{{ $tag }}[{{ $key }}]" value="{{ array_first($file->get_tags()[$tag]) }}" class="form-control form-{{ $tag }}" />
44
@else
55
<ul>
66
@foreach ($file->get_tags()[$tag] as $key => $value)
77
<li>{{ $key }} => {{ $value }}</li>
88
@endforeach
99
</ul>
1010
@endif
11+
@else
12+
<input type="text" name="{{ $tag }}[{{ $key }}]" value="" class="form-control form-{{ $tag }}" />
1113
@endif
1214

1315
@if (isset($file->get_suggested_tags()[$tag]))
1416
<div class="suggested">
1517
@if (count($file->get_suggested_tags()[$tag]) === 1)
16-
{{ array_first($file->get_suggested_tags()[$tag]) }}
18+
<a href="#" class="tag-suggestion">{{ array_first($file->get_suggested_tags()[$tag]) }}</a>
1719
@else
1820
<ul>
1921
@foreach ($file->get_suggested_tags()[$tag] as $key => $value)
20-
<li>{{ $value }}</li>
22+
<li><a href="#" class="tag-suggestion">{{ $value }}</a></li>
2123
@endforeach
2224
</ul>
2325
@endif

resources/views/list_tags.blade.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
@endsection
66

77
@section('scripts')
8+
<script type="text/javascript">
9+
var csrf_token = "{{ csrf_token() }}";
10+
var tagFileUrl = "{{ route('tagFile') }}";
11+
var deleteUrl = "{{ route('deleteFile') }}";
12+
</script>
813
<script type="text/javascript" src="{{ asset(elixir('js/tags.js')) }}"></script>
914
<script type="text/javascript" src="{{ asset(elixir('js/rename.js')) }}"></script>
1015
@endsection
@@ -31,9 +36,16 @@
3136
<th>Action</th>
3237
</tr>
3338

34-
@if ($files)
35-
@each('partials.filetotag', $files, 'file')
36-
@endif
39+
<form method="POST" action="{{ route('tagFiles') }}">
40+
{{ csrf_field() }}
41+
@if ($files)
42+
@each('partials.filetotag', $files, 'file')
43+
@endif
44+
45+
<tr>
46+
<td colspan="13" class="valid-button"><button type="submit" class="btn btn-outline-primary btn-large">Tag all files</button></td>
47+
</tr>
48+
</form>
3749
</tbody>
3850
</table>
3951
@endsection

resources/views/partials/filetomove.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
<td class="large form-group"><input type="text" name="title[{{ $key }}]" value="{{ app('suggester')->get_suggested_name($file) }}" class="form-control form-title" /></td>
66

77
<td class="short centered"><button type="button" onclick="moveAction(this)" class="btn btn-outline-primary btn-sm">Move</button></td>
8-
<td class="short centered"><button type="button" onclick="deleteAction(this)" class="btn btn-outline-danger btn-sm">Delete</button></td>
8+
<td class="short centered"><button type="button" onclick="deleteAction(this, 4)" class="btn btn-outline-danger btn-sm">Delete</button></td>
99
</tr>

resources/views/partials/filetotag.blade.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<tr>
2+
<input type="hidden" name="filepath[{{ $key }}]" value="{{ $file->get_full_path() }}" class="form-filepath" />
3+
24
<td class="image">
35
@if (isset($file->get_tags()['image']))
46
<img src="data:{{ $file->get_tags()['image']['image_mime'] }};base64,{{ base64_encode($file->get_tags()['image']['data']) }}" width="50" height="50" /><br />
@@ -19,7 +21,8 @@
1921
<td class="bpm">@include('helpers.display_tag', ['tag' => 'bpm'])</td>
2022
<td class="key">@include('helpers.display_tag', ['tag' => 'initial_key'])</td>
2123
<td class="action">
24+
<button type="button" onclick="setAllTags(this)" class="btn btn-outline-primary btn-sm">Set</button>
2225
<button type="button" onclick="tagAction(this)" class="btn btn-outline-primary btn-sm">Tag</button>
23-
<button type="button" onclick="deleteAction(this)" class="btn btn-outline-danger btn-sm">Del</button>
26+
<button type="button" onclick="deleteAction(this, 13)" class="btn btn-outline-danger btn-sm">Del</button>
2427
</td>
2528
</tr>

resources/views/rename.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
@each('partials.filetomove', $files, 'file')
4444
<tr>
4545
<td></td>
46-
<td><button type="submit" class="btn btn-outline-primary btn-large ">Move all files</button></td>
46+
<td><button type="submit" class="btn btn-outline-primary btn-large">Move all files</button></td>
4747
<td></td>
4848
<td></td>
4949
</tr>

0 commit comments

Comments
 (0)