Skip to content

Commit

Permalink
Add a route to create the Shared Drives directory (#4359)
Browse files Browse the repository at this point in the history
  • Loading branch information
nono committed Mar 26, 2024
2 parents c1cb0ed + e23bd54 commit 3b9b252
Show file tree
Hide file tree
Showing 11 changed files with 1,045 additions and 917 deletions.
3 changes: 3 additions & 0 deletions assets/locales/de.po
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ msgstr "Fotos"
msgid "Tree Notes"
msgstr "Notizen"

msgid "Tree Shared Drive"
msgstr "Shared drives"

msgid "Tree Shared with me"
msgstr "Posteingang der Freigaben"

Expand Down
3 changes: 3 additions & 0 deletions assets/locales/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ msgstr "Photos"
msgid "Tree Notes"
msgstr "Notes"

msgid "Tree Shared Drive"
msgstr "Shared drives"

msgid "Tree Shared with me"
msgstr "Inbox of sharings"

Expand Down
3 changes: 3 additions & 0 deletions assets/locales/es.po
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ msgstr "Fotos"
msgid "Tree Notes"
msgstr "Notas"

msgid "Tree Shared Drive"
msgstr "Shared drives"

msgid "Tree Shared with me"
msgstr "Compartido conmigo"

Expand Down
3 changes: 3 additions & 0 deletions assets/locales/fr.po
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ msgstr "Photos"
msgid "Tree Notes"
msgstr "Notes"

msgid "Tree Shared Drive"
msgstr "Drives partagés"

msgid "Tree Shared with me"
msgstr "Partages reçus"

Expand Down
3 changes: 3 additions & 0 deletions assets/locales/ja.po
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ msgstr "写真"
msgid "Tree Notes"
msgstr "メモ"

msgid "Tree Shared Drive"
msgstr "Shared drives"

msgid "Tree Shared with me"
msgstr "私と共有"

Expand Down
3 changes: 3 additions & 0 deletions assets/locales/nl_NL.po
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ msgstr "Foto's"
msgid "Tree Notes"
msgstr "Notities"

msgid "Tree Shared Drive"
msgstr "Shared drives"

msgid "Tree Shared with me"
msgstr "Gedeeld met mij"

Expand Down
60 changes: 60 additions & 0 deletions docs/files.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,66 @@ Location: https://cozy.example.com/files/6494e0ac-dfcb-11e5-88c1-472e84a9cbee
**Note**: see [not synchronized directories](not-synchronized-vfs.md) for
more informations about the `not_synchronized_on` field.

### POST /files/shared-drives

This endpoint returns the information about the Shared Drives directory. If the
directory does not exist, it is created.

#### Request

```http
POST /files/shared-drives
Accept: application/vnd.api+json
```

#### Response

```http
HTTP/1.1 201 Created
Content-Type: application/vnd.api+json
```

```json
{
"data": {
"type": "io.cozy.files",
"id": "io.cozy.files.shared-drives-dir",
"meta": {
"rev": "1-e4abdb5a"
},
"attributes": {
"type": "directory",
"name": "Shared drives",
"path": "/Shared drives",
"created_at": "2024-03-25T15:22:00Z",
"updated_at": "2024-03-25T15:22:00Z",
"cozyMetadata": {
"doctypeVersion": "1",
"metadataVersion": 1,
"createdAt": "2024-03-25T15:22:00Z",
"createdByApp": "drive",
"createdOn": "https://cozy.example.com/",
"updatedAt": "2024-03-25T15:22:00Z"
}
},
"relationships": {
"parent": {
"links": {
"related": "/files/io.cozy.files.root-dir"
},
"data": {
"type": "io.cozy.files",
"id": "io.cozy.files.root-dir"
}
}
},
"links": {
"self": "/files/io.cozy.files.shared-drives-dir"
}
}
}
```

### GET /files/:file-id

Get a directory or a file informations. In the case of a directory, it contains
Expand Down
31 changes: 31 additions & 0 deletions model/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ package instance

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"os"
"path"
"strings"
"time"
Expand Down Expand Up @@ -273,6 +275,35 @@ func (i *Instance) ThumbsFS() vfs.Thumbser {
}
}

// EnsureSharedDrivesDir returns the Shared Drives directory, and creates it if
// it doesn't exist
func (i *Instance) EnsureSharedDrivesDir() (*vfs.DirDoc, error) {
fs := i.VFS()
dir, err := fs.DirByID(consts.SharedDrivesDirID)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, err
}
if dir != nil {
return dir, nil
}

name := i.Translate("Tree Shared Drives")
dir, err = vfs.NewDirDocWithPath(name, consts.RootDirID, "/", nil)
if err != nil {
return nil, err
}
dir.DocID = consts.SharedDrivesDirID
dir.CozyMetadata = vfs.NewCozyMetadata(i.PageURL("/", nil))
err = fs.CreateDir(dir)
if errors.Is(err, os.ErrExist) {
dir, err = fs.DirByPath(dir.Fullpath)
}
if err != nil {
return nil, err
}
return dir, nil
}

// NotesLock returns a mutex for the notes on this instance.
func (i *Instance) NotesLock() lock.ErrorRWLocker {
return config.Lock().ReadWrite(i, "notes")
Expand Down
3 changes: 3 additions & 0 deletions pkg/consts/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const (
// NoLongerSharedDirID is the identifier of the directory where the files &
// folders removed from a sharing but still used via a reference are put
NoLongerSharedDirID = "io.cozy.files.no-longer-shared-dir"
// DrivesDirID is the identifier of the directory where the
// (shared|external) drives are saved.
SharedDrivesDirID = "io.cozy.files.shared-drives-dir"
)

const (
Expand Down
16 changes: 16 additions & 0 deletions web/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ const TagSeparator = ","
// recognized
var ErrDocTypeInvalid = errors.New("Invalid document type")

// SharedDrivesCreationHandler is the handler for POST /files/drives. It
// creates the directory where shared and external drives are saved if it
// doesn't exist, and return information about this directory.
func SharedDrivesCreationHandler(c echo.Context) error {
inst := middlewares.GetInstance(c)
if err := middlewares.AllowWholeType(c, permission.POST, consts.Files); err != nil {
return err
}
doc, err := inst.EnsureSharedDrivesDir()
if err != nil {
return wrapVfsError(err)
}
return jsonapi.Data(c, http.StatusOK, newDir(doc), nil)
}

// CreationHandler handle all POST requests on /files/:file-id
// aiming at creating a new document in the FS. Given the Type
// parameter of the request, it will either upload a new file or
Expand Down Expand Up @@ -1900,6 +1915,7 @@ func Routes(router *echo.Group) {
router.PATCH("/:file-id", ModifyMetadataByIDHandler)
router.PATCH("/", ModifyMetadataByIDInBatchHandler)

router.POST("/shared-drives", SharedDrivesCreationHandler)
router.POST("/", CreationHandler)
router.POST("/:file-id", CreationHandler)
router.PUT("/:file-id", OverwriteFileContentHandler)
Expand Down
Loading

0 comments on commit 3b9b252

Please sign in to comment.