func ProcessCardiacMRIZip(zipPath string, db *sqlx.DB) (dicoms []DicomOutput, err error) {
	metadata, err := zipPathToMetadata(zipPath)
	if err != nil {
		return nil, err
	}

	zipName := path.Base(zipPath)

	rc, err := zip.OpenReader(zipPath)
	if err != nil {
		return nil, err
	}

	for _, v := range rc.File {
		if !strings.HasPrefix(v.Name, "manifest") {
			continue
		}

		zippedFile, err := v.Open()
		if err != nil {
			return nil, err
		}

		// Consume the full manifest into a buffer so we can re-read it if it is
		// invalid
		manifestBuffer := &bytes.Buffer{}
		if _, err := io.Copy(manifestBuffer, zippedFile); err != nil {
			return nil, err
		}

		manifestReader := bytes.NewReader(manifestBuffer.Bytes())

		// Try to process it as a CSV
		fields, err := processManifest(manifestReader)
		if err != nil {

			// If that fails, try again with our manual algorithm
			manifestReader.Seek(0, 0)

			fields, err = processInvalidManifest(manifestReader)
			if err != nil {
				return nil, err
			}
		}

		dicoms = make([]DicomOutput, len(fields), len(fields))
		for loc := range fields {
			if loc == 0 {
				// Discard the header
				continue
			}

			dicoms[loc], err = stringSliceToDicomStruct(fields[loc])
			if err != nil {
				return nil, err
			}
			dicoms[loc].SampleID = metadata.SampleID
			dicoms[loc].ZipFile = zipName
			dicoms[loc].FieldID = metadata.FieldID
			dicoms[loc].Instance = metadata.Instance
			dicoms[loc].Index = metadata.Index
		}

		zippedFile.Close()
	}

	deleteEmpty := []int{}
	for i := range dicoms {
		if dicoms[i].SampleID == "" {
			deleteEmpty = append(deleteEmpty, i)
		}
	}

	for loc, i := range deleteEmpty {
		dicoms = append(dicoms[:i-loc], dicoms[i+1-loc:]...)
	}

	return dicoms, nil
}
// Iterating over the directory will be delegated to the caller.
// Here we will process one zip file.
Iterating over the directory will be delegated to the caller.
Here we will process one zip file.
// Iterating over the directory will be delegated to the caller . // Here we will process one zip file .
func ProcessCardiacMRIZip(zipPath string, db *sqlx.DB) (dicoms []DicomOutput, err error) {
	metadata, err := zipPathToMetadata(zipPath)
	if err != nil {
		return nil, err
	}
	zipName := path.Base(zipPath)
	rc, err := zip.OpenReader(zipPath)
	if err != nil {
		return nil, err
	}
	for _, v := range rc.File {
		if !strings.HasPrefix(v.Name, "manifest") {
			continue
		}
		zippedFile, err := v.Open()
		if err != nil {
			return nil, err
		}
		manifestBuffer := &bytes.Buffer{}
		if _, err := io.Copy(manifestBuffer, zippedFile); err != nil {
			return nil, err
		}
		manifestReader := bytes.NewReader(manifestBuffer.Bytes())
		fields, err := processManifest(manifestReader)
		if err != nil {
			manifestReader.Seek(0, 0)
			fields, err = processInvalidManifest(manifestReader)
			if err != nil {
				return nil, err
			}
		}
		dicoms = make([]DicomOutput, len(fields), len(fields))
		for loc := range fields {
			if loc == 0 {
				continue
			}
			dicoms[loc], err = stringSliceToDicomStruct(fields[loc])
			if err != nil {
				return nil, err
			}
			dicoms[loc].SampleID = metadata.SampleID
			dicoms[loc].ZipFile = zipName
			dicoms[loc].FieldID = metadata.FieldID
			dicoms[loc].Instance = metadata.Instance
			dicoms[loc].Index = metadata.Index
		}
		zippedFile.Close()
	}
	deleteEmpty := []int{}
	for i := range dicoms {
		if dicoms[i].SampleID == "" {
			deleteEmpty = append(deleteEmpty, i)
		}
	}
	for loc, i := range deleteEmpty {
		dicoms = append(dicoms[:i-loc], dicoms[i+1-loc:]...)
	}
	return dicoms, nil
}
func ProcessCardiacMRIZip ( zipPath string , db * sqlx . DB ) ( dicoms [ ] DicomOutput , err error ) { metadata , err := zipPathToMetadata ( zipPath ) if err != nil { return nil , err } zipName := path . Base ( zipPath ) rc , err := zip . OpenReader ( zipPath ) if err != nil { return nil , err } for _ , v := range rc . File { if ! strings . HasPrefix ( v . Name , " " ) { continue } zippedFile , err := v . Open ( ) if err != nil { return nil , err } manifestBuffer := & bytes . Buffer { } if _ , err := io . Copy ( manifestBuffer , zippedFile ) ; err != nil { return nil , err } manifestReader := bytes . NewReader ( manifestBuffer . Bytes ( ) ) fields , err := processManifest ( manifestReader ) if err != nil { manifestReader . Seek ( 0 , 0 ) fields , err = processInvalidManifest ( manifestReader ) if err != nil { return nil , err } } dicoms = make ( [ ] DicomOutput , len ( fields ) , len ( fields ) ) for loc := range fields { if loc == 0 { continue } dicoms [ loc ] , err = stringSliceToDicomStruct ( fields [ loc ] ) if err != nil { return nil , err } dicoms [ loc ] . SampleID = metadata . SampleID dicoms [ loc ] . ZipFile = zipName dicoms [ loc ] . FieldID = metadata . FieldID dicoms [ loc ] . Instance = metadata . Instance dicoms [ loc ] . Index = metadata . Index } zippedFile . Close ( ) } deleteEmpty := [ ] int { } for i := range dicoms { if dicoms [ i ] . SampleID == " " { deleteEmpty = append ( deleteEmpty , i ) } } for loc , i := range deleteEmpty { dicoms = append ( dicoms [ : i - loc ] , dicoms [ i + 1 - loc : ] ... ) } return dicoms , nil }
Iterating over the directory will be delegated to the caller.
Iterating over the directory will be delegated to the caller .
// Consume the full manifest into a buffer so we can re-read it if it is // invalid // Try to process it as a CSV // If that fails, try again with our manual algorithm // Discard the header