Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b037345b7b | ||
|
|
a6b4f0fbcf |
No files matched your search
+77
-28
@@ -60,6 +60,14 @@ pub:
|
||||
remove_cover bool
|
||||
}
|
||||
|
||||
// Как прошла запись: на месте (тот же файл) или новым файлом поверх старого
|
||||
pub struct WriteReport {
|
||||
pub:
|
||||
in_place bool
|
||||
// Почему не удалось переписать на месте (пусто, если на месте)
|
||||
fallback string
|
||||
}
|
||||
|
||||
struct ParsedTag {
|
||||
mut:
|
||||
version u8
|
||||
@@ -558,30 +566,74 @@ fn build_tag(parsed ParsedTag, t BookTags, opts WriteOptions) []u8 {
|
||||
return out
|
||||
}
|
||||
|
||||
// Запись тэгов: новый тэг + аудио из исходного файла, через временный файл.
|
||||
// Запись тэгов: новый тэг + аудио из исходного файла собираются во временном файле,
|
||||
// затем копируются обратно в исходный. Файл остаётся тем же (inode, владелец, права):
|
||||
// Audiobookshelf опознаёт файлы по inode, и замена файла новым путает его библиотеку.
|
||||
// Файл без ID3v2 тоже поддерживается — тэг просто ставится в начало
|
||||
fn write_book_tags_impl(path string, t BookTags, opts WriteOptions) ! {
|
||||
fn write_book_tags_impl(path string, t BookTags, opts WriteOptions) !WriteReport {
|
||||
parsed := parse_id3v2(path)!
|
||||
tag := build_tag(parsed, t, opts)
|
||||
|
||||
// Владелец и права исходного файла: перезапись через временный файл иначе их потеряет
|
||||
// (на NAS файлы библиотеки принадлежат пользователю приложения, а агент может работать от root)
|
||||
orig := os.stat(path) or { os.Stat{} }
|
||||
tmp_path := path + '.tagtmp'
|
||||
mut src := os.open(path)!
|
||||
mut dst := os.create(tmp_path)!
|
||||
mut ok := false
|
||||
defer {
|
||||
src.close()
|
||||
dst.close()
|
||||
if !ok {
|
||||
mut buf := []u8{len: 1024 * 1024}
|
||||
{
|
||||
mut src := os.open(path)!
|
||||
defer {
|
||||
src.close()
|
||||
}
|
||||
mut dst := os.create(tmp_path)!
|
||||
defer {
|
||||
dst.close()
|
||||
}
|
||||
dst.write(tag) or {
|
||||
os.rm(tmp_path) or {}
|
||||
return err
|
||||
}
|
||||
src.seek(i64(parsed.audio_start), .start)!
|
||||
for {
|
||||
n := src.read(mut buf) or { 0 }
|
||||
if n <= 0 {
|
||||
break
|
||||
}
|
||||
dst.write(buf[..n]) or {
|
||||
os.rm(tmp_path) or {}
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
copy_back(tmp_path, path, mut buf) or {
|
||||
// Исходный файл мог остаться наполовину переписанным — полная копия во временном
|
||||
reason := err.msg()
|
||||
orig := os.stat(path) or { os.Stat{} }
|
||||
os.mv(tmp_path, path, overwrite: true) or {
|
||||
return error('${err}; новое содержимое сохранено в ${tmp_path}')
|
||||
}
|
||||
if orig.mode != 0 {
|
||||
os.chmod(path, int(orig.mode & 0o7777)) or {}
|
||||
$if !windows {
|
||||
os.chown(path, int(orig.uid), int(orig.gid)) or {}
|
||||
}
|
||||
}
|
||||
return WriteReport{
|
||||
fallback: reason
|
||||
}
|
||||
}
|
||||
os.rm(tmp_path) or {}
|
||||
return WriteReport{
|
||||
in_place: true
|
||||
}
|
||||
}
|
||||
|
||||
dst.write(tag)!
|
||||
src.seek(i64(parsed.audio_start), .start)!
|
||||
mut buf := []u8{len: 1024 * 1024}
|
||||
// Содержимое from — в существующий файл to (с обрезкой), не заменяя сам файл
|
||||
fn copy_back(from string, to string, mut buf []u8) ! {
|
||||
mut src := os.open(from)!
|
||||
defer {
|
||||
src.close()
|
||||
}
|
||||
mut dst := os.open_file(to, 'w')!
|
||||
defer {
|
||||
dst.close()
|
||||
}
|
||||
for {
|
||||
n := src.read(mut buf) or { 0 }
|
||||
if n <= 0 {
|
||||
@@ -589,16 +641,7 @@ fn write_book_tags_impl(path string, t BookTags, opts WriteOptions) ! {
|
||||
}
|
||||
dst.write(buf[..n])!
|
||||
}
|
||||
src.close()
|
||||
dst.close()
|
||||
ok = true
|
||||
if orig.mode != 0 {
|
||||
os.chmod(tmp_path, int(orig.mode & 0o7777)) or {}
|
||||
$if !windows {
|
||||
os.chown(tmp_path, int(orig.uid), int(orig.gid)) or {}
|
||||
}
|
||||
}
|
||||
os.mv(tmp_path, path, overwrite: true)!
|
||||
dst.flush()
|
||||
}
|
||||
|
||||
// ---------- публичный API с защитой от паники ----------
|
||||
@@ -635,8 +678,13 @@ pub fn read_cover(path string) !Cover {
|
||||
|
||||
// Запись тэгов книги в файл
|
||||
pub fn write_book_tags(path string, t BookTags, opts WriteOptions) ! {
|
||||
write_book_tags_report(path, t, opts)!
|
||||
}
|
||||
|
||||
// То же, что write_book_tags, плюс отчёт: переписан ли файл на месте
|
||||
pub fn write_book_tags_report(path string, t BookTags, opts WriteOptions) !WriteReport {
|
||||
mut box := PanicBox{}
|
||||
guard_write_tags(path, t, opts, mut box) or {
|
||||
report := guard_write_tags(path, t, opts, mut box) or {
|
||||
return if box.msg != '' {
|
||||
box.error()
|
||||
} else {
|
||||
@@ -646,6 +694,7 @@ pub fn write_book_tags(path string, t BookTags, opts WriteOptions) ! {
|
||||
if box.msg != '' {
|
||||
return box.error()
|
||||
}
|
||||
return report
|
||||
}
|
||||
|
||||
fn guard_read_tags(path string, mut box PanicBox) !FileTags {
|
||||
@@ -666,11 +715,11 @@ fn guard_read_cover(path string, mut box PanicBox) !Cover {
|
||||
return read_cover_impl(path)!
|
||||
}
|
||||
|
||||
fn guard_write_tags(path string, t BookTags, opts WriteOptions, mut box PanicBox) ! {
|
||||
fn guard_write_tags(path string, t BookTags, opts WriteOptions, mut box PanicBox) !WriteReport {
|
||||
defer {
|
||||
if msg := recover() {
|
||||
box.msg = msg
|
||||
}
|
||||
}
|
||||
write_book_tags_impl(path, t, opts)!
|
||||
return write_book_tags_impl(path, t, opts)!
|
||||
}
|
||||
@@ -131,3 +131,23 @@ fn test_write_keeps_mode() {
|
||||
assert after.mode & 0o777 == before.mode & 0o777
|
||||
assert after.uid == before.uid
|
||||
}
|
||||
|
||||
// Файл остаётся тем же: Audiobookshelf опознаёт файлы по inode
|
||||
fn test_write_keeps_inode() {
|
||||
path := tmp_copy('test_id3v23.mp3')
|
||||
defer {
|
||||
os.rm(path) or {}
|
||||
}
|
||||
before := os.stat(path) or { panic(err) }
|
||||
metadata.write_book_tags(path, metadata.BookTags{
|
||||
title: 'Тот же файл'
|
||||
comment: 'длинное описание '.repeat(500)
|
||||
}) or { panic(err) }
|
||||
after := os.stat(path) or { panic(err) }
|
||||
$if !windows {
|
||||
assert after.inode == before.inode
|
||||
}
|
||||
assert !os.exists(path + '.tagtmp')
|
||||
t := metadata.read_book_tags(path) or { panic(err) }
|
||||
assert t.tags.title == 'Тот же файл'
|
||||
}
|
||||
Reference in new issue
Block a user