Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/controllers/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ class Storage extends Controller {

async get (head = false) {
const version = this.getVersion();
const matchVersion = !!this.request.headers['if-match'];

if (await this.checkToken('r')) {
let numBytesWritten = 0;
try {
var { item, versionMatch } = await this.server._store.get(this._username, this._path, version, head);
var { item, versionMatch } = await this.server._store.get(this._username, this._path, version, matchVersion, head);
} catch (e) {
getLogger().error(`Your storage backend does not behave correctly => ${e.message}`);
this.response.writeHead(500, this._headers);
Expand All @@ -59,7 +61,7 @@ class Storage extends Controller {
}

this.setVersion(item && item.ETag);
if (versionMatch) {
if (!matchVersion && versionMatch) {
delete this._headers['Content-Type'];
this.response.writeHead(304, this._headers);
this.response.end();
Expand All @@ -86,10 +88,14 @@ class Storage extends Controller {
return false;
}
const version = this.getVersion();
const matchVersion = !!this.request.headers['if-match'];

let status, error, created, modified, conflict, isDir;
if (await this.checkToken('w')) {
try {
({ created, modified, conflict, isDir } = await this.server._store.put(this._username, this._path, type, value, version));
({ created, modified, conflict, isDir } = await this.server._store.put(
this._username, this._path, type, value, version, matchVersion
));
status = conflict
? 412
: isDir
Expand Down
10 changes: 8 additions & 2 deletions lib/stores/file_tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,15 @@ class FileTree {
}
}

async put (username, pathname, type, value, version) {
async put (username, pathname, type, value, version, matchVersion) {
const datapath = this.dataPath(username, pathname);
const metapath = this._metaPath(username, pathname);
const basename = decodeURI(path.basename(pathname));
await this._lock(username);
const metadata = await this.readMeta(username, pathname);
let created = false;

if (version) {
if (version && matchVersion) {
if (version === '*'
// check document existence when version '*' specified
? metadata.items && metadata.items[basename]
Expand All @@ -217,6 +217,12 @@ class FileTree {
await this._unlock(username);
return { conflict: true, created };
}
} else if (version && !matchVersion) {
if (metadata.items && metadata.items[basename] &&
version.replace(/"/g, '') === metadata.items[basename].ETag) {
await this._unlock(username);
return { conflict: true, created };
}
}

if (metadata.items[`${basename}/`]) {
Expand Down