github.com/gkstretton/dark/services/goo@v0.0.0-20231114224855-2d1a2074d446/filesystem/paths.go (about)

     1  package filesystem
     2  
     3  import (
     4  	"errors"
     5  	"flag"
     6  	"fmt"
     7  	"os"
     8  	"path/filepath"
     9  	"strconv"
    10  )
    11  
    12  var (
    13  	basePath                    = flag.String("basePath", "/mnt/md0/light-stores/", "base path for storage data")
    14  	contentPath                 = flag.String("sessionPath", "session_content", "path for session content")
    15  	metadataPath                = flag.String("metadataPath", "session_metadata", "path for session metadata")
    16  	rawVideoPath                = flag.String("rawVideoPath", "video/raw", "path within session, of raw video")
    17  	stateReportFileName         = flag.String("stateReportFileName", "state-reports.yml", "filename for list of state reports")
    18  	dispenseMetadataFileName    = flag.String("dispenseMetadataFileName", "dispense-metadata.yml", "filename for dispense metadata")
    19  	vialProfileSnapshotFileName = flag.String("vialProfileSnapshotFileName", "vial-profiles.yml", "filename for vial profiles")
    20  )
    21  
    22  func AssertBasePaths() {
    23  	e := Exists(*basePath)
    24  	if !e {
    25  		panic("base path '" + *basePath + "' does not exist")
    26  	}
    27  }
    28  
    29  func GetBasePath() string {
    30  	return *basePath
    31  }
    32  
    33  func Exists(filepath string) bool {
    34  	_, err := os.Stat(filepath)
    35  	if err == nil {
    36  		return true
    37  	} else if errors.Is(err, os.ErrNotExist) {
    38  		return false
    39  	} else {
    40  		fmt.Printf("unkown Stat error for session base path assertion: %v\n", err)
    41  		return false
    42  	}
    43  }
    44  
    45  func GetMetadataDir() string {
    46  	p := filepath.Join(*basePath, *metadataPath)
    47  	err := os.MkdirAll(p, 0777)
    48  	if err != nil {
    49  		panic(fmt.Errorf("failed to create metadata path: %v", err))
    50  	}
    51  	SetPerms(p)
    52  	return p
    53  }
    54  
    55  func GetStateReportPath(sessionId uint64) string {
    56  	p := filepath.Join(
    57  		*basePath,
    58  		*contentPath,
    59  		strconv.Itoa(int(sessionId)),
    60  	)
    61  	err := os.MkdirAll(p, 0777)
    62  	if err != nil {
    63  		panic(fmt.Errorf("failed to create state report path: %v", err))
    64  	}
    65  	SetPerms(p)
    66  	return filepath.Join(p, *stateReportFileName)
    67  }
    68  
    69  func GetDispenseMetadataPath(sessionId uint64) string {
    70  	p := filepath.Join(
    71  		*basePath,
    72  		*contentPath,
    73  		strconv.Itoa(int(sessionId)),
    74  	)
    75  	err := os.MkdirAll(p, 0777)
    76  	if err != nil {
    77  		panic(fmt.Errorf("failed to create state report path: %v", err))
    78  	}
    79  	SetPerms(p)
    80  	return filepath.Join(p, *dispenseMetadataFileName)
    81  }
    82  
    83  func GetVialProfileSnapshotPath(sessionId uint64) string {
    84  	p := filepath.Join(
    85  		*basePath,
    86  		*contentPath,
    87  		strconv.Itoa(int(sessionId)),
    88  	)
    89  	err := os.MkdirAll(p, 0777)
    90  	if err != nil {
    91  		panic(fmt.Errorf("failed to create state report path: %v", err))
    92  	}
    93  	SetPerms(p)
    94  	return filepath.Join(p, *vialProfileSnapshotFileName)
    95  }
    96  
    97  // GetRawVideoDir mkdirAlls the path if it doesn't exist.
    98  //
    99  //	e.g. 5, top-cam
   100  func GetRawVideoDir(sessionId uint64, rtspPath string) string {
   101  	p := filepath.Join(
   102  		*basePath,
   103  		*contentPath,
   104  		strconv.Itoa(int(sessionId)),
   105  		*rawVideoPath,
   106  		rtspPath,
   107  	)
   108  	err := os.MkdirAll(p, 0777)
   109  	if err != nil {
   110  		panic(fmt.Errorf("failed to create raw video path: %v", err))
   111  	}
   112  	SetPerms(p)
   113  	return p
   114  }
   115  
   116  func GetRawDslrDir(sessionId uint64) string {
   117  	// ensures each level gets set, because this is run from dslrcapture root
   118  	// service.
   119  
   120  	p := filepath.Join(
   121  		*basePath,
   122  		*contentPath,
   123  		strconv.Itoa(int(sessionId)),
   124  		"dslr",
   125  	)
   126  	p2 := filepath.Join(p, "raw")
   127  
   128  	err := os.MkdirAll(p2, 0777)
   129  	if err != nil {
   130  		panic(fmt.Errorf("failed to create raw dslr path: %v", err))
   131  	}
   132  	SetPerms(p)
   133  	SetPerms(p2)
   134  	return p2
   135  }
   136  
   137  func GetPostDslrDir(sessionId uint64) string {
   138  	// ensures each level gets set, because this is run from dslrcapture root
   139  	// service.
   140  
   141  	p := filepath.Join(
   142  		*basePath,
   143  		*contentPath,
   144  		strconv.Itoa(int(sessionId)),
   145  		"dslr",
   146  	)
   147  	p2 := filepath.Join(p, "post")
   148  
   149  	err := os.MkdirAll(p2, 0777)
   150  	if err != nil {
   151  		panic(fmt.Errorf("failed to create post dslr path: %v", err))
   152  	}
   153  	SetPerms(p)
   154  	SetPerms(p2)
   155  	return p2
   156  }
   157  
   158  // GetIncrementalFile considers 'outDir' and returns the **full path to** the
   159  // NEXT incremental file name on disk (w/ .'ext'). E.g:
   160  //
   161  //	1.mp4 2.mp4 3.mp4 -> [outDir]/4.mp4
   162  func GetIncrementalFileName(outDir string, ext string) string {
   163  	i := 1
   164  	for {
   165  		p := filepath.Join(outDir, fmt.Sprintf("%04d", i)+"."+ext)
   166  		if !Exists(p) {
   167  			return p
   168  		}
   169  		i++
   170  		if i > 10000 {
   171  			panic("bug in GetIncrementalFileName: filename should likely not exceed 10000")
   172  		}
   173  	}
   174  }
   175  
   176  func GetLatestDslrFileNumber(sessionId uint64) uint64 {
   177  	outDir := GetRawDslrDir(sessionId)
   178  	ext := "jpg"
   179  	i := uint64(1)
   180  	for {
   181  		p := filepath.Join(outDir, fmt.Sprintf("%04d", i)+"."+ext)
   182  		if !Exists(p) {
   183  			return i - 1
   184  		}
   185  		i++
   186  		if i > 10000 {
   187  			panic("bug in GetLatestDslrFilename: filename should likely not exceed 10000")
   188  		}
   189  	}
   190  }
   191  
   192  func GetKeyValueStorePath() string {
   193  	return filepath.Join(*basePath, "kv")
   194  }