Skip to content

Commit

Permalink
Create missing parent folders when adding file/folder in Remote
Browse files Browse the repository at this point in the history
  • Loading branch information
sebn committed Jul 31, 2017
1 parent b98eef3 commit a1b2a24
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cli/src/remote/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default class Remote implements Side {
log.info({path}, 'Creating folder...')

const [parentPath, name] = conversion.extractDirAndName(doc.path)
const parent: RemoteDoc = await this.remoteCozy.findDirectoryByPath(parentPath)
const parent: RemoteDoc = await this.remoteCozy.findOrCreateDirectoryByPath(parentPath)
let dir: RemoteDoc

try {
Expand Down Expand Up @@ -91,7 +91,7 @@ export default class Remote implements Side {
log.info({path}, 'Uploading new file...')
const stream = await this.other.createReadStreamAsync(doc)
const [dirPath, name] = conversion.extractDirAndName(path)
const dir = await this.remoteCozy.findDirectoryByPath(dirPath)
const dir = await this.remoteCozy.findOrCreateDirectoryByPath(dirPath)

// Emit events to track the upload progress
let info = clone(doc)
Expand Down
15 changes: 15 additions & 0 deletions cli/test/unit/remote/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ import timestamp from '../../../src/timestamp'
import type { Metadata } from '../../../src/metadata'
import type { RemoteDoc, JsonApiDoc } from '../../../src/remote/document'

import MetadataBuilders from '../../builders/metadata'
import configHelpers from '../../helpers/config'
import pouchHelpers from '../../helpers/pouch'
import {
cozy, builders, deleteAll, createTheCouchdbFolder
} from '../../helpers/cozy'

const metadataBuilders = new MetadataBuilders()

describe('Remote', function () {
if (process.env.APPVEYOR) {
it('is unstable on AppVeyor')
Expand Down Expand Up @@ -423,6 +426,12 @@ describe('Remote', function () {
size: '36901'
})
})

it('creates the parent folder when missing', async function () {
const metadata: Metadata = metadataBuilders.fileMetadata().path('foo/bar/qux').build()
await this.remote.addFileAsync(metadata)
await should(cozy.files.statByPath('/foo/bar')).be.fulfilled()
})
})

describe('addFolderAsync', () => {
Expand Down Expand Up @@ -460,6 +469,12 @@ describe('Remote', function () {
_rev: remoteDir._rev
})
})

it('creates the parent folder when missing', async function () {
const metadata: Metadata = metadataBuilders.dirMetadata().path('foo/bar/qux').build()
await this.remote.addFolderAsync(metadata)
await should(cozy.files.statByPath('/foo/bar')).be.fulfilled()
})
})

describe('overwriteFileAsync', function () {
Expand Down

0 comments on commit a1b2a24

Please sign in to comment.