Skip to content

Commit

Permalink
Merge pull request #342 from COS301-SE-2024/feat/backend/migrating-im…
Browse files Browse the repository at this point in the history
…age-storage-to-azureblobs

Feat/backend/migrating image storage to azureblobs
  • Loading branch information
waveyboym authored Aug 24, 2024
2 parents 396ec41 + 03e1545 commit 6bafd23
Show file tree
Hide file tree
Showing 22 changed files with 553 additions and 907 deletions.
73 changes: 59 additions & 14 deletions documentation/occupi-docs/pages/api-documentation/api-usage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ The API also allows you to retrieve information about these resources.
- [Get Notification Settings](#GetNotificationSettings)
- [Upload Profile Image](#UploadProfileImage)
- [Download Profile Image](#DownloadProfileImage)
- [Delete Profile Image](#DeleteProfileImage)
- [Image ID](#ImageID)
- [Upload Image](#UploadImage)
- [Upload Room Image](#UploadRoomImage)
- [Delete Room Image](#DeleteRoomImage)
- [Add Room](#AddRoom)
- [Available slots](#AvailableSlots)

Expand Down Expand Up @@ -738,7 +740,7 @@ This endpoint is used to upload a profile image for a user in the Occupi system.

`POST`

- **Request Form Data**
- **Send request as Form-Data NOT JSON body or url parameters**

- **Content**

Expand Down Expand Up @@ -800,6 +802,44 @@ or

- **Content:** `{ "status": 500, "message": "Internal server error", "error": {"code":"INTERNAL_SERVER_ERROR","details":null,"message":"Internal server error"} }`

### DeleteProfileImage

This endpoint is used to delete a profile image for a user in the Occupi system.

- **URL**

`/api/delete-profile-image`

- **Method**

`DELETE`

- **Request JSON Data**

- **Content**

```json copy
"email": "abcd@gmail.com" // optional if you are logged in
```

or

```
/api/delete-profile-image?email=abcd@gmail.com
```

**Success Response**

- **Code:** 200

- **Content:** `{ "status": 200, "message": "Successfully deleted image!", "data": null }`

**Error Response**

- **Code:** 500

- **Content:** `{ "status": 500, "message": "Internal server error", "error": {"code":"INTERNAL_SERVER_ERROR","details":null,"message":"Internal server error"} }`

### Image ID

This endpoint is used to get the image in the Occupi system given an image ID as a url parameter.
Expand Down Expand Up @@ -828,8 +868,6 @@ or

**the id is the id of the image which you will get when you get a room and is required for all requests on this endpoint**

**note that the higher quality, the longer it will take to download**

**Success Response**

- **Code:** 200
Expand All @@ -842,13 +880,13 @@ or

- **Content:** `{ "status": 500, "message": "Internal server error", "error": {"code":"INTERNAL_SERVER_ERROR","details":null,"message":"Internal server error"} }`

### Upload Image
### Upload Room Image

This endpoint is used to upload an image in the Occupi system. Only Admins can upload images.
This endpoint is used to upload an image for a room in the Occupi system. Only Admins can upload images.

- **URL**

`/api/upload-image`
`/api/upload-room-image`

- **Method**

Expand All @@ -860,6 +898,7 @@ This endpoint is used to upload an image in the Occupi system. Only Admins can u

```copy
"image": image_data, // required
"roomId": "RM000" // required
```

**Success Response**
Expand All @@ -874,32 +913,38 @@ This endpoint is used to upload an image in the Occupi system. Only Admins can u

- **Content:** `{ "status": 500, "message": "Internal server error", "error": {"code":"INTERNAL_SERVER_ERROR","details":null,"message":"Internal server error"} }`

### Upload Room Image
### Delete Room Image

This endpoint is used to upload an image for a room in the Occupi system. Only Admins can upload images.
This endpoint is used to delete an image for a room in the Occupi system. Only Admins can delete images.

- **URL**

`/api/upload-room-image`
`/api/delete-room-image`

- **Method**

`POST`
`DELETE`

- **Request Form Data**
- **Request JSON Data**

- **Content**

```copy
"image": image_data, // required
```json copy
"roomId": "RM000" // required
"imageId": "000000000000000000000" // required
```

or

```
/api/delete-room-image?roomId=RM000&imageId=000000000000000000000
```

**Success Response**

- **Code:** 200

- **Content:** `{ "status": 200, "message": "Successfully uploaded image!", "data": {"id": "000000000000000000000"} }`
- **Content:** `{ "status": 200, "message": "Successfully deleted image!", "data": null }`

**Error Response**

Expand Down
Binary file modified occupi-backend/configs/centrifugo.config.json.gpg
Binary file not shown.
138 changes: 87 additions & 51 deletions occupi-backend/configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,57 +9,61 @@ import (
)

const (
MongodbUsername = "MONGODB_USERNAME"
MongodbPassword = "MONGODB_PASSWORD"
MongodbClusteruri = "MONGODB_CLUSTERURI"
MongodbDbname = "MONGODB_DBNAME"
MongodbStartURI = "MONGODB_START_URI"
Port = "PORT"
LogFileName = "LOG_FILE_NAME"
SMTPHost = "SMTP_HOST"
SMTPPort = "SMTP_PORT"
SMTPPassword = "SMTP_PASSWORD"
SystemEmail = "SYSTEM_EMAIL"
CertificateFilePath = "CERTIFICATE_FILE_PATH"
KeyFilePath = "KEY_FILE_PATH"
GinRunMode = "GIN_RUN_MODE"
TrustedProxies = "TRUSTED_PROXIES"
JwtSecret = "JWT_SECRET"
SessionSecret = "SESSION_SECRET"
OccupiDomains = "OCCUPI_DOMAINS"
Env = "ENV"
OtpExpiration = "OTP_EXPIRATION"
CacheEviction = "CACHE_EVICTION"
OtpGenReqEviction = "OTP_GEN_REQ_EVICTION"
AllowOriginsVal = "ALLOW_ORIGINS"
AllowMethodsVal = "ALLOW_METHODS"
AllowHeadersVal = "ALLOW_HEADERS"
ExposeHeadersVal = "EXPOSE_HEADERS"
Caval = "ALLOW_CREDENTIALS"
MaxAgeVal = "MAX_AGE"
IPCIT = "IP_CLIENT_INFO_TOKEN"
RabbitMQUsername = "RABBITMQ_USERNAME"
RabbitMQPassword = "RABBITMQ_PASSWORD"
RabbitMQHost = "RABBITMQ_HOST"
RabbitMQPort = "RABBITMQ_PORT"
RPID = "RP_ID"
RPName = "RP_NAME"
RPOrigins = "RP_ORIGINS"
CentrifugoAKy = "CENTRIFUGO_API_KEY"
CentrifugoHost = "CENTRIFUGO_HOST"
CentrifugoPort = "CENTRIFUGO_PORT"
NewRelicAppName = "NEW_RELIC_APP_NAME"
SentryDSN = "SENTRY_DSN"
PassPhrase = "TEST_PASS_PHRASE"
NewRelicLicenseKey = "NEW_RELIC_LICENSE_KEY"
MiddlewareService = "MIDDLEWARE_SERVICE"
MiddelwareAT = "MIDDLEWARE_AT"
MiddlewareTarget = "MIDDLEWARE_TARGET"
MiddlewareAgentService = "MW_AGENT_SERVICE"
RedisUsername = "REDIS_USERNAME"
RedisPassword = "REDIS_PASSWORD"
RedisHost = "REDIS_HOST"
RedisPort = "REDIS_PORT"
MongodbUsername = "MONGODB_USERNAME"
MongodbPassword = "MONGODB_PASSWORD"
MongodbClusteruri = "MONGODB_CLUSTERURI"
MongodbDbname = "MONGODB_DBNAME"
MongodbStartURI = "MONGODB_START_URI"
Port = "PORT"
LogFileName = "LOG_FILE_NAME"
SMTPHost = "SMTP_HOST"
SMTPPort = "SMTP_PORT"
SMTPPassword = "SMTP_PASSWORD"
SystemEmail = "SYSTEM_EMAIL"
CertificateFilePath = "CERTIFICATE_FILE_PATH"
KeyFilePath = "KEY_FILE_PATH"
GinRunMode = "GIN_RUN_MODE"
TrustedProxies = "TRUSTED_PROXIES"
JwtSecret = "JWT_SECRET"
SessionSecret = "SESSION_SECRET"
OccupiDomains = "OCCUPI_DOMAINS"
Env = "ENV"
OtpExpiration = "OTP_EXPIRATION"
CacheEviction = "CACHE_EVICTION"
OtpGenReqEviction = "OTP_GEN_REQ_EVICTION"
AllowOriginsVal = "ALLOW_ORIGINS"
AllowMethodsVal = "ALLOW_METHODS"
AllowHeadersVal = "ALLOW_HEADERS"
ExposeHeadersVal = "EXPOSE_HEADERS"
Caval = "ALLOW_CREDENTIALS"
MaxAgeVal = "MAX_AGE"
IPCIT = "IP_CLIENT_INFO_TOKEN"
RabbitMQUsername = "RABBITMQ_USERNAME"
RabbitMQPassword = "RABBITMQ_PASSWORD"
RabbitMQHost = "RABBITMQ_HOST"
RabbitMQPort = "RABBITMQ_PORT"
RPID = "RP_ID"
RPName = "RP_NAME"
RPOrigins = "RP_ORIGINS"
CentrifugoAKy = "CENTRIFUGO_API_KEY"
CentrifugoHost = "CENTRIFUGO_HOST"
CentrifugoPort = "CENTRIFUGO_PORT"
NewRelicAppName = "NEW_RELIC_APP_NAME"
SentryDSN = "SENTRY_DSN"
PassPhrase = "TEST_PASS_PHRASE"
NewRelicLicenseKey = "NEW_RELIC_LICENSE_KEY"
MiddlewareService = "MIDDLEWARE_SERVICE"
MiddelwareAT = "MIDDLEWARE_AT"
MiddlewareTarget = "MIDDLEWARE_TARGET"
MiddlewareAgentService = "MW_AGENT_SERVICE"
RedisUsername = "REDIS_USERNAME"
RedisPassword = "REDIS_PASSWORD"
RedisHost = "REDIS_HOST"
RedisPort = "REDIS_PORT"
AzureAccountName = "AZURE_ACCOUNT_NAME"
AzurePFPContainerName = "AZURE_PFP_CONTAINER_NAME"
AzureRoomsContainerName = "AZURE_ROOMS_CONTAINER_NAME"
AzureAccountKey = "AZURE_ACCOUNT_KEY"
)

// init viper
Expand Down Expand Up @@ -531,3 +535,35 @@ func GetRedisPort() string {
}
return port
}

func GetAzureAccountName() string {
accountName := viper.GetString(AzureAccountName)
if accountName == "" {
accountName = "AZURE_ACCOUNT_NAME"
}
return accountName
}

func GetAzurePFPContainerName() string {
containerName := viper.GetString(AzurePFPContainerName)
if containerName == "" {
containerName = "AZURE_PFP_CONTAINER_NAME"
}
return containerName
}

func GetAzureRoomsContainerName() string {
containerName := viper.GetString(AzureRoomsContainerName)
if containerName == "" {
containerName = "AZURE_ROOMS_CONTAINER_NAME"
}
return containerName
}

func GetAzureAccountKey() string {
accountKey := viper.GetString(AzureAccountKey)
if accountKey == "" {
accountKey = "AZURE_ACCOUNT_KEY"
}
return accountKey
}
Binary file modified occupi-backend/configs/config.yaml.gpg
Binary file not shown.
Binary file modified occupi-backend/configs/dev.deployed.yaml.gpg
Binary file not shown.
Binary file modified occupi-backend/configs/dev.localhost.yaml.gpg
Binary file not shown.
Binary file modified occupi-backend/configs/prod.yaml.gpg
Binary file not shown.
21 changes: 21 additions & 0 deletions occupi-backend/configs/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net"
"time"

"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
"github.com/allegro/bigcache/v3"
"github.com/centrifugal/gocent/v3"
"github.com/go-webauthn/webauthn/webauthn"
Expand Down Expand Up @@ -272,3 +273,23 @@ func CreateMailServerConnection() *gomail.Dialer {

return d
}

func CreateAzureBlobClient() *azblob.Client {
url := fmt.Sprintf("https://%s.blob.core.windows.net/", GetAzureAccountName())

credential, err := azblob.NewSharedKeyCredential(GetAzureAccountName(), GetAzureAccountKey())

if err != nil {
logrus.Fatal("Unable to create azure credentials due to error: ", err)
}

client, err := azblob.NewClientWithSharedKeyCredential(url, credential, nil)
if err != nil {
logrus.Fatal("Unable to create azure credentials due to error: ", err)
}

fmt.Println("Azure blob storage connection created!")
logrus.Info("Azure blob storage connection created!")

return client
}
Binary file modified occupi-backend/configs/test.yaml.gpg
Binary file not shown.
9 changes: 8 additions & 1 deletion occupi-backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ require (
)

require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.13.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.7.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.4.0 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
Expand All @@ -54,8 +59,10 @@ require (
github.com/grafana/pyroscope-go v1.1.0 // indirect
github.com/grafana/pyroscope-go/godeltaprof v0.1.6 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/philhofer/fwd v1.1.2 // indirect
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
github.com/redis/go-redis/v9 v9.6.1 // indirect
github.com/tinylib/msgp v1.1.9 // indirect
github.com/umahmood/haversine v0.0.0-20151105152445-808ab04add26 // indirect
Expand Down Expand Up @@ -128,7 +135,7 @@ require (
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
Expand Down
Loading

0 comments on commit 6bafd23

Please sign in to comment.