file

package
v0.0.0-...-cda7898 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 26, 2021 License: Apache-2.0 Imports: 26 Imported by: 1

Documentation

Overview

Package file is used to handle all file-service and download-service related operations. Handling is implemented with a HTTP router returned from NewRouter and setup its routes using Setup.

Index

Constants

View Source
const (
	// ParamFileParent is a constant for file parent parameter in a request.
	ParamFileParent = "parent"

	// ParamFileName is a constant for file name parameter in a request.
	ParamFileName = "name"

	// ParamFileType is a constant for file type parameter in a request.
	ParamFileType = "type"

	// ParamFileDescription is a constant for file description parameter in a request.
	ParamFileDescription = "description"

	// ParamFileSize is a constant for file size parameter in a request.
	ParamFileSize = "size"

	// ParamFileCreatedAt is a constant for file created at parameter in a request.
	ParamFileCreatedAt = "createdAt"

	// ParamFileID is the name of the file id param in URL.
	ParamFileID = "id"

	// ParamFileUpdatedAt is a constant for file updated at parameter in a request.
	ParamFileUpdatedAt = "updatedAt"

	// ParamPageNum is a constant for the requested page num in the pagination.
	ParamPageNum = "pageNum"

	// ParamPageSize is a constant for the requested page size in the pagination.
	ParamPageSize = "pageSize"

	// QueryShareFiles is the querystring key for retrieving the files that were shared with the user.
	QueryShareFiles = "shares"

	// QueryAppID is a constant for queryAppId parameter in a request.
	// If exists, the files returned will only belong to the app of QueryAppID.
	QueryAppID = "appId"

	// QueryFileDownloadPreview is the querystring key for
	// removing the content-disposition header from a file download.
	QueryFileDownloadPreview = "preview"

	// OwnerRole is the owner role name when referred to as a permission.
	OwnerRole = "OWNER"

	// GetFileByIDRole is the role that is required of the authenticated requester to have to be
	// permitted to make the GetFileByID action.
	GetFileByIDRole = ppb.Role_READ

	// GetFilesByFolderRole is the role that is required of the authenticated requester to have to be
	// permitted to make the GetFilesByFolder action.
	GetFilesByFolderRole = ppb.Role_READ

	// DeleteFileByIDRole is the role that is required of the authenticated requester to have to be
	// permitted to make the DeleteFileByID action.
	DeleteFileByIDRole = ppb.Role_READ

	// DownloadRole is the role that is required of the authenticated requester to have to be
	// permitted to make the Download action.
	DownloadRole = ppb.Role_READ

	// UpdateFileRole is the role that is required of the authenticated requester to have to be
	// permitted to make the UpdateFile action.
	UpdateFileRole = ppb.Role_WRITE

	// UpdateFilesRole is the role that is required of the authenticated requester to have to be
	// permitted to make the UpdateFiles action.
	UpdateFilesRole = ppb.Role_WRITE

	// PdfMimeType is the mime type of a .pdf file.
	PdfMimeType = "application/pdf"

	// TextMimeType is the start of any file with a text mime type.
	TextMimeType = "text"

	// DocMimeType is the mime type of a .doc file.
	DocMimeType = "application/msword"

	// DocxMimeType is the mime type of a .docx file.
	DocxMimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"

	// XlsMimeType is the mime type of a .xls file.
	XlsMimeType = "application/vnd.ms-excel"

	// XlsxMimeType is the mime type of a .xlsx file.
	XlsxMimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

	// PptMimeType is the mime type of a .ppt file.
	PptMimeType = "application/vnd.ms-powerpoint"

	// PptxMimeType is the mime type of a .pptx file.
	PptxMimeType = "application/vnd.openxmlformats-officedocument.presentationml.presentation"

	// RtfMimeType is the mime type of a .rtf file.
	RtfMimeType = "application/rtf"

	// OdtMimeType is the mime type of a .odt file.
	OdtMimeType = "application/vnd.oasis.opendocument.text"

	// OdpMimeType is the mime type of a .odp file.
	OdpMimeType = "application/vnd.oasis.opendocument.presentation"
)

Variables

View Source
var (
	// TypesConvertableToPdf is a slice of the names of the mime types that can be converted to PDF and previewed.
	TypesConvertableToPdf = []string{
		DocMimeType,
		DocxMimeType,
		XlsMimeType,
		XlsxMimeType,
		PptMimeType,
		PptxMimeType,
		RtfMimeType,
		OdtMimeType,
		OdpMimeType,
	}

	// AllowedAllOperationsApps are the applications which are allowed to do any operation
	// open to external apps on files which are not theirs
	AllowedAllOperationsApps = []string{oauth.DriveAppID}

	// AllowedDownloadApps are the applications which are only allowed to download
	// files which are not theirs
	AllowedDownloadApps = []string{oauth.DriveAppID, oauth.DropboxAppID, oauth.CargoAppID}
)

Functions

func AddPermissionsOnError

func AddPermissionsOnError(c *gin.Context,
	err error,
	fileID string,
	permissions []*ppb.GetFilePermissionsResponse_UserRole,
	permissionClient ppb.PermissionClient,
	logger *logrus.Logger)

AddPermissionsOnError add the deleted permissions when the file is failed to delete

func CheckUserFilePermission

func CheckUserFilePermission(ctx context.Context,
	fileClient fpb.FileServiceClient,
	permissionClient ppb.PermissionClient,
	userID string,
	fileID string,
	role ppb.Role) (string, *ppb.PermissionObject, error)

CheckUserFilePermission checks if userID is permitted to fileID with the wanted role. The function returns the role name if the user is permitted to the file, the permission if the user was shared, and non-nil err if any encountered. If no permitted then role found role would be "". If user was shared then permission would be non-nil.

func CheckUserFileTransfer

func CheckUserFileTransfer(ctx context.Context,
	dropboxClient drp.DropboxClient,
	userID string,
	fileID string,
	role ppb.Role) (bool, error)

CheckUserFileTransfer checks if userID is has a transfer to fileID. The function returns true if the user has a transfer to the file and nil error, otherwise false and non-nil err if any encountered.

func CreatePermission

func CreatePermission(ctx context.Context,
	fileClient fpb.FileServiceClient,
	permissionClient ppb.PermissionClient,
	userID string,
	permission ppb.PermissionObject) error

CreatePermission creates permission in permission service only if userID has ppb.Role_WRITE permission to permission.FileID.

func DeleteFile

func DeleteFile(ctx *gin.Context,
	logger *logrus.Logger,
	fileClient fpb.FileServiceClient,
	uploadClient upb.UploadClient,
	searchClient spb.SearchClient,
	permissionClient ppb.PermissionClient,
	fileID string,
	userID string) ([]string, error)

DeleteFile deletes fileID from file service and upload service, returns a slice of IDs of the files that were deleted if there were any files that are descendants of fileID and any error if occurred. nolint: gocyclo

func HandleStream

func HandleStream(c *gin.Context, stream dpb.Download_DownloadClient) error

HandleStream streams the file bytes from stream to c.

func HandleUpdate

func HandleUpdate(
	ctx context.Context,
	ids []string,
	pf partialFile,
	fileClient fpb.FileServiceClient,
	uploadClient upb.UploadClient,
	searchClient spb.SearchClient,
	logger *logrus.Logger) ([]*fpb.FailedFile, error)

HandleUpdate updates many files with the same value. The function gets slice of ids and the partial file to update. It returns the updated file id's, and non-nil error if occurred.

func IsFileConvertableToPdf

func IsFileConvertableToPdf(contentType string) bool

IsFileConvertableToPdf returns true if contentType can be converted to a PDF file, false otherwise.

Types

type GetFileByIDResponse

type GetFileByIDResponse struct {
	ID          string      `json:"id,omitempty"`
	Name        string      `json:"name,omitempty"`
	Type        string      `json:"type,omitempty"`
	Size        int64       `json:"size"`
	Description string      `json:"description,omitempty"`
	OwnerID     string      `json:"ownerId,omitempty"`
	Parent      string      `json:"parent,omitempty"`
	CreatedAt   int64       `json:"createdAt,omitempty"`
	UpdatedAt   int64       `json:"updatedAt,omitempty"`
	Role        string      `json:"role,omitempty"`
	Shared      bool        `json:"shared"`
	Permission  *Permission `json:"permission,omitempty"`
	IsExternal  bool        `json:"isExternal"`
	AppID       string      `json:"appID,omitempty"`
}

GetFileByIDResponse is a structure used for parsing fpb.File to a json file metadata response.

func CreateGetFileResponse

func CreateGetFileResponse(file *fpb.File, role string, permission *ppb.PermissionObject) *GetFileByIDResponse

CreateGetFileResponse Creates a file grpc response to http response struct.

type Permission

type Permission struct {
	UserID  string `json:"userID,omitempty"`
	FileID  string `json:"fileID,omitempty"`
	Role    string `json:"role,omitempty"`
	Creator string `json:"creator,omitempty"`
}

Permission is a struct that describes a user's permission to a file.

type Router

type Router struct {
	// contains filtered or unexported fields
}

Router is a structure that handles upload requests.

func NewRouter

func NewRouter(
	fileConn *grpcPoolTypes.ConnPool,
	downloadConn *grpcPoolTypes.ConnPool,
	uploadConn *grpcPoolTypes.ConnPool,
	permissionConn *grpcPoolTypes.ConnPool,
	dropboxConn *grpcPoolTypes.ConnPool,
	searchConn *grpcPoolTypes.ConnPool,
	gotenbergClient *gotenberg.Client,
	oAuthMiddleware *oauth.Middleware,
	logger *logrus.Logger,
) *Router

NewRouter creates a new Router, and initializes clients of File Service and Download Service with the given connections. If logger is non-nil then it will be set as-is, otherwise logger would default to logrus.New().

func (*Router) DeleteFileByID

func (r *Router) DeleteFileByID(c *gin.Context)

DeleteFileByID is the request handler for DELETE /files/:id request.

func (*Router) Download

func (r *Router) Download(c *gin.Context)

Download is the request handler for /files/:id?alt=media request.

func (*Router) GetFileAncestors

func (r *Router) GetFileAncestors(c *gin.Context)

GetFileAncestors returns an array of the requested file ancestors. The function gets an id. It returns the updated file id's.

func (*Router) GetFileByID

func (r *Router) GetFileByID(c *gin.Context)

GetFileByID is the request handler for GET /files/:id

func (*Router) GetFilesByFolder

func (r *Router) GetFilesByFolder(c *gin.Context)

GetFilesByFolder is the request handler for GET /files request.

func (*Router) GetSharedFiles

func (r *Router) GetSharedFiles(c *gin.Context, queryAppID string)

GetSharedFiles is the request handler for GET /files?shares. Currently, can only be requested by AllowedAllOperationsApps. queryAppID is the specific app requested by the application.

func (*Router) HandlePreview

func (r *Router) HandlePreview(c *gin.Context, file *fpb.File, stream dpb.Download_DownloadClient) error

HandlePreview writes a PDF of the file to the response, if the file isn't a PDF already and can be converted to a PDF, then the file would be converted to a PDF and the converted file will be written to the response, instead of the raw file.

func (*Router) HandleUserFilePermission

func (r *Router) HandleUserFilePermission(
	c *gin.Context,
	fileID string,
	role ppb.Role) (string, *ppb.PermissionObject)

HandleUserFilePermission gets the id of the requested file, and the required role. Returns the user role as a string, and the permission if the user is permitted to operate on the file, and `"", nil` if not.

func (*Router) HandleUserFilePermit

func (r *Router) HandleUserFilePermit(
	c *gin.Context,
	fileID string,
	role ppb.Role) bool

HandleUserFilePermit gets a gin context and the id of the requested file, returns true if the user is permitted to operate on the file. Returns false if the user isn't permitted to operate on it, Returns false if any error occurred and logs the error.

func (*Router) Setup

func (r *Router) Setup(rg *gin.RouterGroup)

Setup sets up r and initializes its routes under rg.

func (*Router) UpdateFile

func (r *Router) UpdateFile(c *gin.Context)

UpdateFile Updates single file. The function gets an id as a parameter and the partial file to update. It returns the updated file id.

func (*Router) UpdateFiles

func (r *Router) UpdateFiles(c *gin.Context)

UpdateFiles Updates many files with the same value. The function gets slice of ids and the partial file to update. It returns the updated file id's.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL