// buildFile saves a data file from the tar archive to disk, // then returns a struct with data we'll need to construct the // GenericFile object in Fedora later. func (reader *Reader) saveWithChecksums(gf *models.GenericFile) { // Set up a MultiWriter to stream data ONCE to file, // md5 and sha256. We don't want to process the stream // three separate times. err := os.MkdirAll(filepath.Dir(gf.IngestLocalPath), 0755) if err != nil { gf.IngestErrorMessage = err.Error() return } outputWriter, err := os.OpenFile(gf.IngestLocalPath, os.O_CREATE|os.O_WRONLY, 0644) if outputWriter != nil { defer outputWriter.Close() } if err != nil { gf.IngestErrorMessage = fmt.Sprintf("Error opening %s for writing: %v", gf.IngestLocalPath, err) return } md5Hash := md5.New() shaHash := sha256.New() multiWriter := io.MultiWriter(md5Hash, shaHash, outputWriter) io.Copy(multiWriter, reader.tarReader) gf.IngestMd5 = fmt.Sprintf("%x", md5Hash.Sum(nil)) gf.IngestSha256 = fmt.Sprintf("%x", shaHash.Sum(nil)) gf.IngestSha256GeneratedAt = time.Now().UTC() gf.FileFormat, _ = platform.GuessMimeType(gf.IngestLocalPath) // on err, defaults to application/binary return }
// For partner build, we just want to make sure this doesn't throw // an execption. For our own server build, we also want to make sure // it gets the mime type right. GuessMimeType is defined in mime.go, // with a dummy version in partner builds in nomime.go. func TestGuessMimeType(t *testing.T) { pathToTempFile := setupMimeTest(t) defer teardownMimeTest(pathToTempFile) mimetype, err := platform.GuessMimeType(pathToTempFile) if err != nil { t.Error(err) } if !platform.IsPartnerBuild { assert.Equal(t, "text/plain", mimetype) } }