old file deletion

This commit is contained in:
Muaz Ahmad 2023-09-22 12:54:50 +05:00
parent 77c36a689a
commit e05180baf3
3 changed files with 23 additions and 5 deletions

View file

@ -31,6 +31,7 @@ type SRTManager struct {
bw uint32 bw uint32
mtu uint32 mtu uint32
output io.WriteCloser output io.WriteCloser
stream_key string
} }
func NewSRTManager(l net.PacketConn) (*SRTManager) { func NewSRTManager(l net.PacketConn) (*SRTManager) {
@ -145,7 +146,8 @@ func (agent *SRTManager) process_conclusion(packet *Packet) (*Packet) {
for _, v := range hs_cif.hs_extensions { for _, v := range hs_cif.hs_extensions {
switch v.ext_type { switch v.ext_type {
case 5: case 5:
writer, ok := CheckStreamID(v.ext_contents.([]byte)) writer, stream_key, ok := CheckStreamID(v.ext_contents.([]byte))
agent.stream_key = stream_key
if !ok { if !ok {
resp_packet.cif.(*HandshakeCIF).hs_type = 1003 resp_packet.cif.(*HandshakeCIF).hs_type = 1003
return resp_packet return resp_packet

View file

@ -5,6 +5,8 @@ import (
"io" "io"
"strings" "strings"
"stream_server/transcoder" "stream_server/transcoder"
"time"
"path/filepath"
) )
func NewWriter(stream_key string) (io.WriteCloser, error) { func NewWriter(stream_key string) (io.WriteCloser, error) {
@ -15,7 +17,20 @@ func NewWriter(stream_key string) (io.WriteCloser, error) {
return transcoder_in, nil return transcoder_in, nil
} }
func CheckStreamID(stream_id []byte) (io.WriteCloser, bool) { func CleanFiles(stream_key string, delay time.Duration) {
time.Sleep(delay * time.Second)
base_dir, _ := os.UserHomeDir()
stream_dir := base_dir + "/live/" + stream_key
fileinfo, _ := os.Stat(stream_dir + "/stream.m3u8")
if time.Now().Sub(fileinfo.ModTime()) > delay * time.Second {
leftover_files, _ := filepath.Glob(stream_dir + "/*")
for _, file := range leftover_files {
os.Remove(file)
}
}
}
func CheckStreamID(stream_id []byte) (io.WriteCloser, string, bool) {
stream_key := make([]byte, 0) stream_key := make([]byte, 0)
for i := len(stream_id) - 1; i >= 0; i-- { for i := len(stream_id) - 1; i >= 0; i-- {
if stream_id[i] == 0 { if stream_id[i] == 0 {
@ -27,11 +42,11 @@ func CheckStreamID(stream_id []byte) (io.WriteCloser, bool) {
if check_stream_key(stream_key_string) { if check_stream_key(stream_key_string) {
writer, err := NewWriter(stream_key_string) writer, err := NewWriter(stream_key_string)
if err != nil { if err != nil {
return nil, false return nil, stream_key_string, false
} }
return writer, true return writer, stream_key_string, true
} }
return nil, false return nil, stream_key_string, false
} }
func check_stream_key(stream_key string) (bool) { func check_stream_key(stream_key string) (bool) {

View file

@ -48,6 +48,7 @@ func (tunnel *Tunnel) Shutdown() {
tunnel.WritePacket(packet) tunnel.WritePacket(packet)
if tunnel.state.output != nil { if tunnel.state.output != nil {
tunnel.state.output.Close() tunnel.state.output.Close()
go CleanFiles(tunnel.state.stream_key, 10)
} }
} }
} }