-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Combine PREMIS files from Vecteur AIPs
- Loading branch information
Showing
7 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<premis:premis version="3.0" | ||
xmlns="http://www.loc.gov/premis/v3" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.loc.gov/premis/v3 http://www.loc.gov/standards/premis/premis.xsd"> | ||
</premis:premis> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package activities | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"os" | ||
"path" | ||
"strings" | ||
|
||
"github.com/beevik/etree" | ||
"github.com/otiai10/copy" | ||
) | ||
|
||
const CombinePREMISName = "combine-premis" | ||
|
||
type CombinePREMISActivity struct{} | ||
|
||
func NewCombinePREMISActivity() *CombinePREMISActivity { | ||
return &CombinePREMISActivity{} | ||
} | ||
|
||
type CombinePREMISParams struct { | ||
Path string | ||
} | ||
|
||
type CombinePREMISResult struct { | ||
Out string | ||
} | ||
|
||
func (md *CombinePREMISActivity) Execute( | ||
ctx context.Context, | ||
params *CombinePREMISParams, | ||
) (*CombinePREMISResult, error) { | ||
// Get transfer's PREMIS file paths. | ||
file_paths, err := CombinePREMISGetPaths(params.Path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Copy empty PREMIS file into metadata directory. | ||
source_filepath := "empty_premis.xml" | ||
dest_filepath := path.Join(params.Path, "metadata/premis.xml") | ||
|
||
err = copy.Copy(source_filepath, dest_filepath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Write elements from transfer's PREMIS files to combined PREMIS file. | ||
combined_premis_filepath := path.Join(params.Path, "metadata/premis.xml") | ||
|
||
for _, file_path := range file_paths { | ||
err := CombinePREMISCopy(file_path, combined_premis_filepath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
res := &CombinePREMISResult{} | ||
res.Out = "OK" | ||
return res, nil | ||
} | ||
|
||
func CombinePREMISGetPaths(transfer_dir string) ([]string, error) { | ||
dir_items, err := os.ReadDir(transfer_dir) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
file_paths := []string{} | ||
for _, dir_item := range dir_items { | ||
if dir_item.IsDir() { | ||
subdir := path.Join(transfer_dir, dir_item.Name()) | ||
|
||
sub_items, err := os.ReadDir(subdir) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, subdir_item := range sub_items { | ||
if !subdir_item.IsDir() { | ||
if strings.HasSuffix(strings.ToLower(subdir_item.Name()), "_premis.xml") { | ||
file_paths = append(file_paths, path.Join(subdir, subdir_item.Name())) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return file_paths, nil | ||
} | ||
|
||
func CombinePREMISCopy(source_filepath string, destination_filepath string) error { | ||
// Parse source document and get root PREMIS element. | ||
source_doc := etree.NewDocument() | ||
|
||
if err := source_doc.ReadFromFile(source_filepath); err != nil { | ||
return err | ||
} | ||
|
||
source_premis_element := source_doc.FindElement("/premis") | ||
if source_premis_element == nil { | ||
return errors.New("no root premis element found in source document") | ||
} | ||
|
||
// Read source child PREMIS elements. | ||
source_premis_object_elements := source_premis_element.FindElements("object") | ||
source_premis_event_elements := source_premis_element.FindElements("event") | ||
source_premis_agent_elements := source_premis_element.FindElements("agent") | ||
|
||
// Parse destination document and get root PREMIS element. | ||
dest_doc := etree.NewDocument() | ||
if err := dest_doc.ReadFromFile(destination_filepath); err != nil { | ||
return err | ||
} | ||
|
||
dest_premis_element := dest_doc.FindElement("/premis") | ||
if dest_premis_element == nil { | ||
return errors.New("no root premis element found in destination document") | ||
} | ||
|
||
// Update PREMIS originalname child elements of PREMIS object elements. | ||
for _, premis_object_element := range source_premis_object_elements { | ||
objectname_element := premis_object_element.FindElement("originalName") | ||
if objectname_element != nil { | ||
objectname_element.SetText("data/" + objectname_element.Text()) | ||
} | ||
} | ||
|
||
// Write destination child PREMIS elements. | ||
CombinePREMISAddChildElements(dest_premis_element, source_premis_object_elements) | ||
CombinePREMISAddChildElements(dest_premis_element, source_premis_event_elements) | ||
CombinePREMISAddChildElements(dest_premis_element, source_premis_agent_elements) | ||
|
||
dest_doc.Indent(2) | ||
err := dest_doc.WriteToFile(destination_filepath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func CombinePREMISAddChildElements(parent_element *etree.Element, new_child_elements []*etree.Element) { | ||
for _, child_element := range new_child_elements { | ||
child_element.Space = "premis" | ||
for _, element := range child_element.FindElements("//*") { | ||
element.Space = "premis" | ||
} | ||
parent_element.AddChild(child_element) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters