Skip to content

Commit

Permalink
Merge pull request #296 from xhyqaq/feat/video-duration
Browse files Browse the repository at this point in the history
feat: add user-video-duration
  • Loading branch information
IRONICBo authored Mar 14, 2024
2 parents c646bba + f4b8451 commit a91a91e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cmd/gopcomm/yap/user_yap.html
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,11 @@ <h1 class="text-4xl"> Have no data!</h1>
</n-card>
</n-modal>

<!-- Duration -->
<n-tag :bordered="false" size="small">
${ item.Duration }
</n-tag>

<!-- Update Time -->
<span class="absolute bottom-4" style="color: #A0AEC0;">
${ item.CreateAt.slice(0,10) }
Expand Down
21 changes: 21 additions & 0 deletions internal/core/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"database/sql"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"math"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -150,6 +152,12 @@ func (c *Community) SaveMedia(ctx context.Context, userId string, data []byte, f
c.xLog.Warn(err.Error())
return 0, err
}
duration, err = formatVideoDuration(duration)
c.xLog.Info(duration)
if err != nil {
c.xLog.Warn(err.Error())
return 0, err
}
}
// save
stem, err := c.db.Prepare(`insert into file (file_key,format,size,user_id,create_at,update_at,duration) VALUES (?,?,?,?,?,?,?)`)
Expand All @@ -167,6 +175,19 @@ func (c *Community) SaveMedia(ctx context.Context, userId string, data []byte, f
return id, err
}

func formatVideoDuration(duration string) (string, error) {
seconds, err := strconv.ParseFloat(duration, 64)
if err != nil {
return "", err
}

minutes := math.Floor(seconds / 60)
secondsRemainder := int(seconds) % 60

formattedTime := fmt.Sprintf("%d:%02d", int(minutes), secondsRemainder)
return formattedTime, nil
}

// for internal use,no need to add ctx
func (c *Community) getMediaInfo(fileKey string) (*File, error) {
r, err := c.S3Service.NewReader(context.Background(), fileKey, nil)
Expand Down

0 comments on commit a91a91e

Please sign in to comment.